javac -classpath和-sourcepath的区别
今天在看Apache Ant的"Tutorial: Hello World with Apache Ant"一文的时候,对javac和java命令的用法不是很理解。于是,又检索到了下面这篇文章,全文转自 “Using classpath and sourcepath"。
链接:
Setting The CLASSPATH
The CLASSPATH setting can be used to 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 you DO use either option, then the current directory WILL NOT be included in the class path.Click here for a more in-depth discussion on setting the class path.
If the -sourcepath option is not specified, the user class path is searched for source files as well as class files.
Setting The 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.
Sourcepath is similar to classpath, the difference being the sourcepath contains .java files and the classpath contains .class files. They both represent search paths for dependencies.
For example, say one has the directory structure:
foo/src/TestFoo.java baz/src/TestBaz.javawith class TestFoo:package com.foo.test; public class TestFoo {;}and class TestBaz:package com.baz.test; import com.foo.test.TestFoo; class TestBaz extends TestFoo {;}These two classes have a dependency: TestBaz requires TestFoo to compile. To compile TestBaz, the following would NOT work:javac -d baz/classes baz/src/TestBaz.javabecause the compiler would not be able to find TestFoo So, you have two options for compiling TestBaz.
- The way most of us are accustomed to: compile TestFoo, then add it to the classpath while compiling TestBaz
javac -d foo/classes foo/src/TestFoo.java javac -d baz/classes -classpath foo/classes baz/src/TestBaz.java
- Add TestFoo to the sourcepath while compiling TestBaz
javac -d baz/classes -sourcepath foo/src baz/src/TestBaz.java(Actually, I lied. You really have a third option. Put dependencies in both sourcepath and classpath. Then, if the .class file in the classpath goes out of date, the .java file in sourcepath will be recompiled. See Sun's javac page for a description of how sourcepath and classpath work together.)
Separating Source Files and Class Files
It often makes sense to keep source files and class files in separate directories, especially on large projects. We use -d to indicate the separate class file destination. Since the source files are not in the user class path, we use -sourcepath to help the compiler find them.C:> dir classes\ lib\ src\ C:> dir src farewells\ C:> dir src\farewells Base.java GoodBye.java C:> dir lib Banners.jar C:> dir classes C:> javac -sourcepath src -classpath classes;lib\Banners.jar \ src\farewells\GoodBye.java -d classes C:> dir classes farewells\ C:> dir classes\farewells Base.class GoodBye.classNote that the compiler compiledsrc\farewells\Base.java
, even though we didn't specify it on the command line. To trace automatic compiles, use the -verbose option.