19

A have a Java code as:

public class Hello
{
        public void print()
        {
                System.out.println("Hi");
        }
}

I compiled it and created a Hello.class. I added it to a Jar file hello.jar as:

$jar -cvf hello.jar Hello.class

I wrote one more program as:

class Test1
{
        public static void main(String[] args)
        {
                new Hello().print();
                System.out.println(Hello.class.getClassLoader());
        }
}

And deleted Hello.class from current directory.

Then I copied hello.jar in extension class path. My program works fine as:

$sudo cp hello.jar /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext
$ java Test1
Hi
sun.misc.Launcher$ExtClassLoader@28d93b30
$ 

If I delete hello.jar from extension class path and copy it in boot strap class path (usr/lib/jvm/java-8-openjdk-amd64/jre/lib/) which contains rt.jar too, then my program is not working.

$ java Test1
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
    at Test1.main(Test1.java:5)
Caused by: java.lang.ClassNotFoundException: Hello
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

According to my knowledge all jar files in bootstrap or extension class loader can be used in program. Why does Java throw that Exception if it's correct? If I am not correct then please guide me.

Joost Verbraeken
  • 883
  • 2
  • 15
  • 30
my name is GYAN
  • 1,269
  • 13
  • 27

3 Answers3

7

The Bootstrap classes are very specific by default, they only include classes of rt.jar and several other important jar files, as it is protected and not meant to be extended only Extension classes are meant to be extended by adding jar files into jre/lib/ext/.

However you can modify the classpath defining the Bootstrap classes on Open JDK by launching your JVM as next:

java -Xbootclasspath/a:"/path/to/my/folder/classes" Test1

-Xbootclasspath/a:path

Specify a colon-separated path of directires, JAR archives, and ZIP archives to append to the default bootstrap class path.

-Xbootclasspath/p:path

Specify a colon-separated path of directires, JAR archives, and ZIP archives to prepend in front of the default bootstrap class path. Note: Applications that use this option for the purpose of overriding a class in rt.jar should not be deployed as doing so would contravene the Java 2 Runtime Environment binary code license.

Here is a concrete example https://wiki.openjdk.java.net/display/mlvm/BootClassPath

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
4

How the Java Launcher Finds Bootstrap Classes

Bootstrap classes are the classes that implement the Java 2 Platform. Bootstrap classes are in the rt.jar and several other jar files in the jre/lib directory. These archives are specified by the value of the bootstrap class path which is stored in the sun.boot.class.path system property. This system property is for reference only, and should not be directly modified.

It is very unlikely that you will need to redefine the bootstrap class path. The nonstandard option, -Xbootclasspath, allows you to do so in those rare circumstances in which it is necessary to use a different set of core classes.

Above statements would provide you answers for your question.

[Check Oracle Doc]

System.out.println(System.getProperty("sun.boot.class.path"));

C:\Program Files\Java\jdk1.8.0_74\jre\lib\resources.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\rt.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\sunrsasign.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\jsse.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\jce.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\charsets.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\lib\jfr.jar;
C:\Program Files\Java\jdk1.8.0_74\jre\classes
Gopidoss
  • 254
  • 1
  • 6
4

I am using java 7 not 8, i hope this is fine for all the versions of java.

in java 7 if you go to this lib folder you can see a file named classlist, if you open it, you can see all the basic classes are listed there(around 2202 classes). These classes are loaded from all the jar files inside lib folder. These folders contains only the JAVA apis. So in order to access your jar you must include that in the classlist file

Anoop LL
  • 1,548
  • 2
  • 21
  • 32