Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between classpath and sourcepath options of javac

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.

like image 241
Alex Avatar asked Mar 14 '10 09:03

Alex


People also ask

What is the difference between Sourcepath and classpath?

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.

What is classpath in javac?

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.

What is Sourcepath?

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.

What is javac option?

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 .


1 Answers

 -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.

like image 131
stacker Avatar answered Sep 18 '22 18:09

stacker