Netbeans Jar file not executing
I have been working on a project using Netbeans (7.4 Development Version), and I want to test the jar distributable. When I use 'java -jar filename.jar', the program works fine. But when I try to double-click the jar file, I get this kind of error: Error: Could not find or load main class W:\pathtoproject\dist\jarname.jar.
This doesn't make sense, I've never seen an error where the jar file thinks it is the main class. I defined the main class in the project properties. I extracted the jar file and saw that the Manifest file has the correct main class defined. How can I fix this error?
Edit: The manifest in the jar file has this:
Manifest-Version: 1.0 Ant-Version: Apache Ant 1.9.1 Created-By: 1.7.0_21-b11 (Oracle Corporation) Class-Path: X-COMMENT: Main-Class will be added automatically by build Main-Class: Driver
Driver does indeed have the usual main method.
Edit 2: I don't know if it makes a difference, but my driver and some other important classes are not part of any package. Only helper classes are packaged. I was trained to code the hard way (i.e. notepad and cmd), so I'm not used to using all the features that Netbeans has to offer ( such as packaging).
The option that NetBeans provides to make executable jars is actually quite helpful. The only better tool for making jar files that I have found is the wizard to make jar files in Eclipse. I strongly recommend that you use one of these tools.
The Eclipse executable jar creation wizard is worth downloading Eclipse for. After using the wizard once, you can save an ANT script so that you can go back to using a command line if you desire. The Eclipse wizard can, do advanced and incredibly useful tasks such as packaging your code and all of it's required libraries into one stand-alone executable jar file.
To get to the wizard in Eclipse, go to "File" -> "Export..." -> "Java" -> "Runnable JAR File".
If you still want to use NetBeans go to "Run" -> "Set Project Configuration" -> "Customize..." -> "Build" -> "Packaging" and select "Build JAR after Compiling". You may have to do a clean build before it makes the jar. Your jar will show up in the "dist" folder in the root of your project files.
For a jar be executable, you have to be concerned with two thing on the manifest file:
- The 'Main-Class' entry must be pointing to the class that'll be launching the application, i.e. the class where you declared the main method to start your app.
- After all entries on the manifest file, you have to leave three lines in blank (I never found a documentation explaining why that is required but always works for me).
You can use this tool to check if the jar file generated by Netbeans complies with that.
Hope I have helped.
It turns out that my work computer was the problem. A company setting was making the jar file not execute properly. When I tried the jar on my personal laptop, it worked fine. From there I just wrapped it into an executable and went on my merry way.