Hello!

Here's the problem:
After making some GUI application (NetBeans) in output directory (containing generated .jar file) also appeared a directory 'lib' with additional .jar. This jar is some extra library with classes, which path is added in manifest ("Class-Path: lib/bla.jar").

So I've used to pack my applications always intto a single .jar file, so it's easier to move, but with this one I've got a problem.

Without this .jar application gets error "ClassNotFoundException", so I want to load the classes from that jar in runtime as a resource. I've learned the way to load a single class by:

URL jar = getClass().getResource("lib/swing.jar");
URLClassLoader ucl = new URLClassLoader(new URL[] {jar});
url.loadClass("org.jdesktop.layout.Baseline");

And here's the question: how to load ALL of the classes in package 'org.jdesktop.layout' in that jar, because 'org.jdesktop.layout.*' doesn't work. Any way to explore this directory to get all of the paths like 'org.jdesktop.layout.GroupLayout' to load all classes?

Sry for a bit long post and waiting for advices. :)

Recommended Answers

All 5 Replies

This doesn't answer your question directly, but as an alternative, you can have the JVM load the classes from your jar file using

java -Djava.ext.dirs=lib\swing.jar; <your class here>

Indeed, it doesn't. Still looking for suggestions to solve my problem...

I am using Eclipse, and when using its export features I have always managed to export everything into one jar file.
Also - a quick search came up with this: http://one-jar.sourceforge.net/, might help.

The code in this article shows how to iterate over the resources inside a jar.

Found on Google with keywords
java jar introspection

Thx, kramerd! That's exactly what I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.