I am writing a program that connects to a web host that has a directory containing a couple JAR files. Then, a GUI shows the JAR name and when you double click on the name, it will open the JAR file.
The problem that I am having is, not all of the JAR files have the main method in the same place, so I need some way of finding the main class.
I have tried doing an approach like this:
File file = new File("website/test.jar");
JarFile jar = new JarFile(file);
String mainClass = jar.getManifest().getMainAttributes().get("Main-Class").toString();
However, I get:
Exception in thread "main" java.util.zip.ZipException: error in opening zip file
On the line where a JarFile object is created. I have thought of two solutions: go through all the class files in the JAR and search for the one that contains the main method, or create a text file in the directory that tells the main file of each JAR.
I would prefer to use the first solution, because it does not actually require me to do extra steps every time I upload another JAR. However, I am concerned that it is kind of a “brute-force” alternative, and it feels inefficient.
Have any of you experienced a similar problems?
Thanks a lot!
See this :
http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html