javac 的-sourcepath这个参数怎么用的

yak 发布于 2014/02/06 21:34
阅读 703
收藏 0

命令帮助显示的是    -sourcepath <路径> 指定查找输入源文件的位置

假设当前路径是 proj 
 要编译 proj\src\java\main\org\Server.java 的源文件

在当前路径 运行javac -sourcepath  src\java\main  org\Server.java 提示

错误:无法读取: org\Server.java
 1 错误

那这个 -sourcepath 参数是用来做什么用的?

加载中
0
NealFeng
NealFeng
-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: Classes found through the class path may be subject to automatic recompilation if their sources are also found. See Searching For Types.

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html



0
yak
yak

引用来自“NealFeng”的答案

-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: Classes found through the class path may be subject to automatic recompilation if their sources are also found. See Searching For Types.

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html



    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.
% ls
classes/ lib/ src/
% ls src
farewells/
% ls src/farewells
Base.java GoodBye.java
% ls lib
Banners.jar
% ls classes
% javac -sourcepath src -classpath classes:lib/Banners.jar \
src/farewells/GoodBye.java -d classes
% ls classes
farewells/
% ls classes/farewells
Base.class GoodBye.class

Note: The compiler compiled src/farewells/Base.java, even though we didn't specify it on the command line. To trace automatic compiles, use the -verbose option.  按照上面的例子写了两个程序,按照命令运行javac并不编译Base.java 加-verbose参数 输出是 [已写入RegularFileObject[classes\GoodBye.class]] 加不加    -sourcepath src 对结果没什么影响 只是-verbose参数会显示 [源文件的搜索路径: src]  源文件不是写在命令行的吗,怎么还要搜索呢  


NealFeng
NealFeng
看SEARCHING FOR TYPES那段。因为要编译的类中需要的一些其它类(比如父类,变量),但这些类定义不在要编译的类的源文件中,也没有在命令行列出,这时编译器可以自动到sourcepath指定的路径下去找这些类型的源文件,并自动编译。
返回顶部
顶部