2

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).

2
  • What's it say in the manifest.mf file inside the jar?
    – Lee Meador
    Aug 1, 2013 at 20:27
  • It will be easier to read if you edit the question and add it there. So the class "Driver" with no package has a main method?
    – Lee Meador
    Aug 1, 2013 at 20:31

4 Answers 4

0

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.

1
  • I already had the "Build JAR after Compiling" checked. Netbeans was already BUILDING the jar, that wasn't a problem. The problem was that the jar wasn't working. Turns out it was my computer, not the JAR.
    – TheDuke777
    Aug 9, 2013 at 21:24
0

For a jar be executable, you have to be concerned with two thing on the manifest file:

  1. 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.
  2. 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.

2
  • You can check the content of the manifest file simply unziping it, as well. Aug 9, 2013 at 1:24
  • I knew about the main class bit, but I've never heard about that second part. As it turns out, the jar file was fine. It was my computer that was causing the problem. I suspect the problem was one of the company settings on my work computer. When I tried the jar file on my laptop, it worked fine.
    – TheDuke777
    Aug 9, 2013 at 21:21
0

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.

0

Your project must contain two or three java files, one of which is the java file and the other one is the frame.java file having the frame coding.

Create the object of your frame file in the java file under the same project, and then clean and build your project.

For example if you have your project name as project11, and under it you have two files, named project11.java and project11frame.java, then create the object in project11.java:

project11frame pf = new project11frame();
pf.setVisible(true); 

Then clean and build your project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.