Please, imagine a folder which has two files
- MyApp.jar
- B.class (real path is long)
Inside the jar there is some code to check whether B.class exists inside jar-file.
try {
Class.forName("B");
System.out.println("exists");
} catch (Exception ignored) {
System.out.println("does not exist");
}
But even though B.class is not inside jar, the code above does not throw exception, because B.class exists outside of jar.
The jar is generated with Ant from Eclipse. So I thought class-path might be the reason
<manifest>
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="org.client.Client"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ many_jar_here.jar"/>
</manifest>
So I have changed only Class-Path like this
<attribute name="Class-Path" value=""/>
But now it gives an error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: B
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRs
der.java:56)
Caused by: java.lang.ClassNotFoundException: B
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
In this scenario, where B is found by the class loader but it’s not in the jar, you could additionally check if
or
If either of those is true, B was loaded from MyApp.jar.
So, something like this: