-sourcepath vs -classpath

女生的网名这么多〃 提交于 2019-12-22 05:54:31

问题


Studying for the oracle certification I am trying all the possible scenarios that might occur during the exam. For example, here there is a little doubt about shell command line (unix based):

Let's imagine there is a folder called myProject and a sub folder called myProject/source.

File SubFile.java is in the folder myProject/source and another file File.java is in myProject folder.

By typing the following commands I get to different behaviors:

cd source (therefore, currently I am on "myProject/source")

javac -sourcepath ../ File.java
// The command ../ does not work to access "Folder" then after compiling File.java from myProject folder and returning to the sub Folder if I try:

javac -classpath ../ SubFile.java

// with the flag -classpath it seems to accept the ../ syntax to access the super folder.

Do you know why it works like this? and moreover is there any chance to access the super folder with the -sourcepath flag?


回答1:


It depends on whether SubFile also references File.

Consider the following code:

public class SubFile {
    private static File file = new File();
}

Assumed that this file is located in your source folder, and assumed that you are in the source folder, then

javac -sourcepath ../ SubFile.java

will compile SubFile.java into SubFile.class inside the source folder, and will compile File.java into File.class in the parent folder. If there is no dependency between those files, then the compiler will not compile File.java (means, the compiler will not automatically compile all files on the sourcepath).

When compiling with -classpath, then the classpath is also searched for source files, unless you explicitly specify a separate sourcepath - in the following case, the compiler will throw an error (assumed that you have cleaned the File.class file before):

javac -classpath .. -sourcepath \temp SubFile.java

See also javac - Java programming language compiler and Differences between classpath and sourcepath options of javac for more information.

The important point from these two links is:

Note: Classes found through the class path may be subject to automatic recompilation if their sources are also found.



来源:https://stackoverflow.com/questions/15087224/sourcepath-vs-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!