Creating class instances from JAR file entires?

Hi all,

I want to distribute my plugins to my app as JAR files. Each plugin can have one or more classes in it that the plugin requires. What I want to be able to do is load the JAR file, and be able to construct class instances from those in the JAR file. I see the JarFile class, and JarFileEntry and stuff, but there doesn't seem to be anything that creates an instance of a class from any .class file in the JAR file. I do see the getInputStream() call. Is this what is needed? You have to read all the bytes of the class out of the JAR file? What about in the case of a ZIP file where compression is done? Does the ZipFile class handle the decompression of all files in the ZIP file so if you do need to use the input stream to read the files and create class instances from, it automatically decompresses the file?

Thanks.

[855 byte] By [buckman1] at [2007-9-27 14:59:54]
# 1
Hi!Just look to my reply to your Class.forName() not loading my class... post.The code there does what you want.
ezavalla at 2007-7-5 23:00:12 > top of Java-index,Core,Core APIs...
# 2

I am not sure how your code there loads classes out of a JAR file? I want to basically "extract" the classes to create new instances of them and in such a way that the main plugin class in the JAR file is able to see all the other classes in the JAR file as it relies on them.

Sadly, I learned that using a separate class loader instance for each plugin would make it impossible for each plugin to access another plugin loaded by another classloader. Apparently the only way is if any class from one plugin is used by another plugin, both plugin classloaders need to load the same files, thus resulting in two (or more depending on how many plugins rely on a certain plugin) of the same classes. Any idea on how to circumvent that problem? I don't think it can be done. Read an article recently that said two of the biggest issues that should result in a new Java 3 code (thus breaking a number of Java 2 code) would be a better classpath feature, and far better class loader support. I'd imagine one of the things they mean is the ability to share files across classloader instances, which a file counting system. Meaning, if two instances of class loaders require the same files, a reference counter for each is incremented and they both point to the same one class. This way, sharing data between them is possible, and all the issues regarding class loader instances require the same classpath for two classes loaded by different instances to see each other.

buckman1 at 2007-7-5 23:00:12 > top of Java-index,Core,Core APIs...
# 3

Sorry, got off topic there. The JAR file issue, it seems there are a few methods in the JarFile class. One is entries(), another is getEntry(), and then there is getInputStream(). There is nothing like createClassInstanceFromEntryInJarFile(). Basically, I want to NOT unjar the files in the JAR file but create instances of those classes at runtime as if they were loaded by the JVM when the app starts. So, do I need to use the getInputStream() of a getEntry() return object to read its class bytes out of the JAR, then call createClassInstance() or something to create a valid class instance? I have yet to find anything that shows how to read classes out of a JAR file and create instances of them without having to unjar the jar file.

buckman1 at 2007-7-5 23:00:12 > top of Java-index,Core,Core APIs...
# 4
Man I have been trying to find out how to post an original post on this forums board, and I have not been successful yet!!! How the heck is it that we post original forums on this thing? Please help.
tugando at 2007-7-5 23:00:12 > top of Java-index,Core,Core APIs...
# 5

Does anyone know how to create an instance of a class at RunTime? For example, I want to load classes from a JAR file at RunTime, and then create instances of certain classes of each JAR. I ask this because I do not see how to create an instance of one of those classes the traditional way, SomeClass var = new SomeClass(). I am pretty sure that someone out there has done this and succeeded in doing it. All of the post on this stuff only talk about loading the class from a JAR file, but I have already loaded them using URLClassLoader抯 findClass() method. I have also created an instance of the class that findClass() returns using newInstance(), but newInstance() returns an object of type Object. How can I convert this object to an object of type SomeClass? You cannot cast it because the compiler will complain due to the import statement issue. So if you cannot include the import statement because that classpath does not exist, then how the heck would you cast the newInstance() object into a SomeClass object?

tugando at 2007-7-5 23:00:12 > top of Java-index,Core,Core APIs...