How does the java compiler find class files, while the classpath is not set to the jdk path?

I am trying to look under the hood about java compilation. So I laid out my IDE and started using the MS-DOS command line ...

I created a simple project, as described below in the tree:

Sampleample

|____**src** |_____pack |______Sample.java |____**classes** 

This is the source code for Sample.java:

 public class Sample { private String s = new String("Hello, world"); public Sample(){ System.out.println(s); } } 

I just want to compile this class, so I used the javac command:

 prompt\SampleApp\src>javac -d ..\classes -sourcepath . pack\Sample.java 

Everything works fine, but I did not expect this because I deleted the CLASSPATH environment variable before compiling my Sample.java file. Therefore, I was expecting a compiler error due to the fact that the compiler could not find the java.lang.String class file.

I read this article http://www.ibm.com/developerworks/java/library/j-classpath-windows/ , which helped me understand a lot. The author of the article says that the default path is the current working directory. But I do not understand why my source code compiles without errors. Can anyone explain this to me?

+7
source share
2 answers

So, I was expecting a compilation error due to the fact that the compiler could not find the java.lang.String class file.

The short answer is that the compiler knows where to find all the standard Java SE library classes without reporting it.

The longer answer is that the String class is in the bootclasspath. This is implicitly set by the javac command to reference the appropriate JAR files in the JDK installation. The javac command searches the bootclasspath before it searches for material in the normal class path.

+6
source

The classpath variable does not do what you think. To list the oracle documentation:

The CLASSPATH variable is one way to tell applications, including the JDK Tools, where to look for custom classes. (Classes that are part of the JRE platform, JDK, and extensions must be defined through others such as the bootstrap class path or the extensions directory.)

Source: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Basically, since java.lang.* Is part of the platform and comes with the JDK / JRE, the compiler does not need to tell you where to look for them.

+5
source

All Articles