Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying a jar file in bootstrap class path not working

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.

like image 411
my name is GYAN Avatar asked Aug 29 '16 13:08

my name is GYAN


People also ask

What is bootstrap classpath?

By default, the bootstrap classpath consists of the "rt. jar" file and some other important JAR files that are supplied by the JRE installation. These provide all of the classes in the standard Java SE class library, along with various "internal" implementation classes.

How do I import a class from a specific jar file?

One option is to call File. listFiles() on the File that denotes the folder, then iterate the resulting array. To traverse trees of nested folders, use recursion. Scanning the files of a JAR file can be done using the JarFile API ... and you don't need to recurse to traverse nested "folders".


2 Answers

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

like image 107
Nicolas Filotto Avatar answered Sep 22 '22 12:09

Nicolas Filotto


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

like image 22
Anoop LL Avatar answered Sep 20 '22 12:09

Anoop LL