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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With