So I made this (very simple) program with a swing GUI with NetBeans, and I clicked build to make a jar file. When I run it by double clicking it, it tells me it could not find the main class, which, after checking, I am sure is definitely there. But, when I run it from Command Prompt, it works perfectly. Any easily-determinable reason for this strange behaviour (if you want the source code, I can post it here)?
Share
The things that seem to be needed in NetBeans are:
Then when you right click on your project and do a “Clean and Build”, a jar will get built into the
distsubdirectory.If that fails to fix the problem, here’s a longer story…
When you double click a jar file to run it, the operating system acts as if you had typed this from the command line:
(When you say it works for you from the command line, is this what you’re typing?)
At that point, the Java executable looks for a file inside the jar named
META-INF/MANIFEST.MF. And then in the contents of that file, it looks for the value of a property,Main-Class. And finally it looks for the class of that name in your jar and runs its staticmain(String[])method.So if your jar is failing to run, you can do the following to debug what’s going on:
cdinto thedistsubdirectory of your project.jar tf filename.jarto list what’s in there.MANIFEST.MFfile is correct:cdinto thedistdirectory.jar xf filename.jar META-INF/MANIFEST.MFto extract the manifest.type META-INF\MANIFEST.MF) and make sureMain-Classis set to the appropriate class.If all of the above check out, then double clicking the file should work.