java – -sourcepath vs -classpath

Learning Oracle certification, I am trying all possible situations that may occur during the exam

Let's assume that there is a folder named myproject and a subfolder named myproject / source

File SubFile. Java is located in the myproject / source folder and another file is file Java is located in the myproject folder

I encounter different behaviors by typing the following command:

CD source (so I'm currently in "myproject / source")

javac -sourcepath ../ File. Java / / command/ Unable to access folder and compile file from myproject folder Java and return to the subfolder, if I try:

javac -classpath ../ SubFile. java

//Using the flag - classpath, it seems to accept/ Syntax to access the super folder

Do you know why it does this? And is there a chance to use the - sourcepath flag to access the super folder?

Solution

This depends on whether subfile also references file

Consider the following codes:

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

Assume that this file is in the source folder and that you are in the source folder

javac -sourcepath ../ SubFile.java

Set subfile Java is compiled into subfile. Java in the source folder Class, and file Java compiled into the file in the parent folder Class If there are no dependencies between these files, the compiler will not be able to compile file Java (meaning that the compiler does not automatically compile all files on the source path)

When compiling with - classpath, in addition to explicitly specifying a separate source path, the source file is also searched in the classpath – the compiler will throw an error in the following cases (assuming that you have cleaned up the file.class file before):

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

For more information, see also javac – Java programming language compiler and differences between classpath and sourcepath options of javac

The two links focus on:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>