I am writing a program that connects to a web host that has a directory containing a couple of JAR files. Then, the JAR name is displayed in the graphical interface, and when you double-click on the name, it will open the JAR file.
The problem I am facing is not that all JAR files have a main method in the same place, so I need to find a way for the main class.
I tried to do this approach:
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
In the line where the JarFile object is created. I thought of two solutions: look through all the class files in the JAR and find 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 in fact I do not need to take extra steps every time I load a different JAR. However, I am concerned that this is a kind of βbrute force,β and it seems ineffective.
Have any of you experienced similar problems? Many thanks!
source share