I read the Sun documentation and a lot of posts on Stack Overflow, but I'm still confused about the differences between the Java compiler options -cp
and -sourcepath
.
Let say I have this directory structure:
c:\Java\project1\src (where the Java source files are) c:\Java\project1\bin (where the Java class files will be or already are)
Let's also say I have a source file MainClass.java
in a package com.mypackage
, and that the directory structure is ok in the source folder.
I'm in the project1 directory and run:
javac -d bin -sourcepath src src/com/mypackage/MainClass.java
or
javac -d bin -classpath src src/com/mypackage/MainClass.java
and I obtain the same result. In verbose mode, the search path for source files is src
in both cases.
It would be great if anybody could help me figure out the specifics of these options.
Sourcepath tells javac where to grab the source code from. Classpath tells javac where to grab other class files that will allow your source code to compile.
Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.
The source path specifies the directories where the C and C++ source files are located. If you are debugging a user-mode process on the computer where the executable file was built, and if the source files are still in their original location, the debugger can automatically locate the source files.
Description. The javac command reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. The javac command can also process annotations in Java source files and classes. There are two ways to pass source code file names to javac .
-classpath classpath
Set the user class path, overriding the user class path in the CLASSPATH environment variable. If neither CLASSPATH or -classpath is specified, the user class path consists of the current directory.
If the -sourcepath option is not specified, the user class path is searched for source files as well as class files.
-sourcepath sourcepath
Specify the source code path to search for class or interface definitions. As with the user class path, source path entries are separated by semicolons (;) and can be directories, JAR archives, or ZIP archives. If packages are used, the local path name within the directory or archive must reflect the package name.
Note that classes found through the classpath are subject to automatic recompilation if their sources are found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With