Home > Net >  How do I access .class files in a zip?
How do I access .class files in a zip?

Time:07-27

I have a zip file "items.zip".

In this zip-File there are different classes, some of them are usfull to me and some are not.

items.zip
    -> circle.class
    -> circleButYouCantDrawIt.class
    // someotherclasses, some drawable, some not

Circle.class has a construcor and a paint-method.

circleButYouCantDrawIt.class has a broken constructor.

Before I start my main programm I want to access items.zip, check through them if they are usable and then be able to use the usefull ones whenever I can. What would be the best way to do this?

I have like 9 attempts to this until now and none of them make sense. How can I do this the best?

CodePudding user response:

Java can use these classes directly if the zip file is put on the classpath when your program starts (as Jar files are zip files with extra information).

If you know the names of the classes ahead of time, you can simply try to create an instance of each using new myclass() (if they have the default constructor).

If not you can scan through items.zip file using the ZIP file handling routines, and then invoke the class as it is still on the classpath.

If you do not want to put it on the classpath yourself, but only do it at runtime if appropriate, you need to look into writing a custom classloader which is probably a bigger hammer than needed.

  • Related