Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to look under the hood about java compilation. So I put my IDE away and started using MS-DOS command-line...

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

SampleApp

|____**src**

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

This is the Sample.java source code :

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

All works fine, but i didn't expect that because I deleted my CLASSPATH environment variable before compiling my Sample.java file. So I was expecting a compiler error due to the fact that the compiler would not be able to 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 many things. The article author says that the default classpath is the current working directory. But I don't understand why my source code compile without error. Could someone explain this to me?

like image 568
Patrick B. Avatar asked Dec 03 '12 03:12

Patrick B.


People also ask

What is difference between CLASSPATH and path in Java?

PATH is used by CMD prompt to find binary files. CLASSPATH is used by the compiler and JVM to find library files.

How does Java search the CLASSPATH?

By default, javac and javadoc search the user class path for both class files and source code files. If the -sourcepath option is specified, javac and javadoc search for source files only on the specified source file path, while still searching the user class path for class files.

Why do we need to set classpath in Java?

You need to set the CLASSPATH if: You need to load a class that is not present in the current directory or any sub-directories. You need to load a class that is not in a location specified by the extensions mechanism.

What is the difference between CLASSPATH and build path?

Answer. Build path is used by the compiler to resolve dependencies and build a project. Classpath is used during runtime when running a project in order to find necessary dependencies. Build path is configured on the Java Build Path property page of a project.


1 Answers

So I was expecting a compiling error due to the fact that the compiler would not be able to find the java.lang.String class file.

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

The longer answer is that String class is being found on the bootclasspath. This is implicitly set by the javac command to refer to the relevant JARs in the JDK installation. The javac command searches the bootclasspath before it looks for stuff on the regular classpath.

like image 80
Stephen C Avatar answered Sep 20 '22 02:09

Stephen C