I want to load classes that are inside a jar, that is located within another jar. Following is a hierarchy tree illustrating the location of the .class.
A.jar
|---A.class
|---B.jar
|---InterfaceA.class
So I have a class 'A'. This class implements an interface, 'Interface B' which is inside B.jar. A third class named 'C' loads at runtime an instance of A.class. However, I end up with a java.lang.ClassNotFoundException. Any idea on how to fix this issue? I've seen plenty of posts that explains how to dynamically load classes from jar files, however I haven't seen anything that explains how to load classes from a jar file that is within another jar file.
Thanks for your time
Related
I tried to obtain a .png image from inside the classpath of my .jar file, so that wouldn't depend on a separate resource folder next to the .jar. I tried doing that with the help of the Class.class.getResourceAsStream(String) method. However, even after ensuring that the path to the .png image is valid, the method still returns null. The path to the file is:
source/org/linear/blank.png
The package also contains two additional files related to my project: the Application class and the Console interface.
As Class class is loaded by the bootstrap classloader, and the Java documentation says that
If this object was loaded by the bootstrap class loader, the method
delegates to ClassLoader.getSystemResourceAsStream
And this method looks for resources in the resources folder.
So to make your code work you have two choices:
1) Use one of your classes, for example, Application, instead of Class. In this case the resource path is resolved relatively to the location of the Application class:
Application.class.getResourceAsStream("/blank.png")
2) Put your image in the resources folder, in this case getSystemResourceAsStream will find it too:
Class.class.getResourceAsStream("/blank.png")
I'm currently making a Java program that loads classes from a different package. It allows users to delete .class files from a package and prevent a crash. I therefore want to load all these classes using:
Class.forName("this.is.a.package.RandomClass");
The only problem is that when I try that it says that the class is not found. If I put the class in the same package it works, but if I wanted to load it from the package "this.is.a" Class.forName("this.is.a.package.RandomClass"), it doesn't work. Any help appreciated!
I know I should probably be using URLClassLoaders but I don't know how I would use them to load a file inside a zipped jar.
I have EAR which contains multiple jars out of which two of the jar contains "log.properties"
For eg: In abc.jar, "log.properties" is in com.abc.test
and in xyz.jar, "log.properties" is in com.xyz.test
In both the package we have logger implementation which load "log.properties" as
this.getClass().getClassLoader().getResourceAsStream("log.properties");
Due to classloader which loads the other jar first it pickup his "log.properties"
I want to avoid this problem
Any suggestions ?
Use ClassLoader.getResources(String name) and write code to filter out URLs that are not in the same package as the class. Place that code in a utility or resource manager class and use it everywhere in your project.
I want to ask about UrlClassLoaders. I write a main .jar file which works well. Now I want to make it able to load plugins from one of its folders. I wrote it until that it finds the .jar files in the folder, can list their classes inside (their name, path). The problem is when I ask him to find that class (for create an instance of it) they won't find the class which it inherites from my main jar file like this:
IN MAIN JAR:
-class Expansion { ... }
In a plugin jar:
-class ExpansionA extends MainJar.Expansion { ... }
So when I ask an UrlClassLoader to resolve the "ExpansionA" class it will search for the "Expansion" class in the plugin jar instead of the MainJar, and there is no "Expansion" class in the plugin jar because it inherites from the MainJar as a resource library... and throws NoDefFoundForClassError exception while the defineClass method. How can I tell him to check after the classes in the MainJar too? (Its not only about 1 class but I want the plugin to be able to inherite any class from the MainJar). Can u help me?
ClassLoaders have a parent to which load requests are delegated first. To ensure that the new class loader you are creating can access the class Expansion you should set the parent of the new ClassLoader to the ClassLoader of your class Expansion:
ClassLoader myLoader=newURLClassLoader(urls, Expansion.class.getClassLoader());
This does not only ensure that the Expansion class will be found by myLoader, it will also guaranty that it resolves to the same runtime class (as the parent is asked first). This is important if the plugin jar contains a copy of the class.
Lets say we want to package our Java application into a jar file (including the dependencies). The dependencies are copied into one separate directory (lets say libs/). I have read that are two approaches to make the java launcher find these classes:
1) Implement a custom class loader code (loader/launcher pattern) that will load necessary classes before the main code of the application is executed (it is described here: http://vladimirvivien.wordpress.com/2011/10/07/86/)
2) Add the libs/ directory to the "Class-Path:" header in meta-inf/manifest.fm
Which solution is better and why?