Search code examples
javacompilationjavac

javac -sourcepath option not working


I am at C:\ and from here I want to compile my Bingo.java source file which resides in C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src\bingo\Bingo.java. If I understand -sourcepath properly, it is used to tell the java compiler where to look for .java source files.

I tried the following to set -sourcepath but none seem to work:

C:\>javac -sourcepath C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src\
bingo\Bingo.java

C:\>javac -sourcepath C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src\bingo\ Bingo.java

Even these...

C:\>javac -sourcepath C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src bingo\Bingo.java

C:\>javac -sourcepath C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src\bingo Bingo.java

All above give the File not found error.

I would like to mention that compiling without -sourcepath from the same location works fine:

C:\>javac C:\Users\Devashish\Documents\NetBeansProjects\Bingo\src\bingo\Bingo.java

I believe I'm doing something very stupid here but can't figure it out. Any help would be appreciated.


Solution

  • -sourcepath defines OTHER .java files that should be in your project. for example:

    You have "C:\A.java":

    public class A {
        public static void main(String args[]) {
           B.hello();
        }
    }
    

    And C:\dir\B.java:

    public class B {
        public void hello() {
            System.out.println("Hello!");
        }
    }
    

    In this case, when you compile and run you need sourcepath to C:\dir\B.java.

    EDIT: Classpath (-cp) is for .class, Sourcepath (-sourcepath) is for .java.