This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
So, if you are here in this thread, you are probably getting the error "Error: Could not find or load the main class (name).jar", or your .jar is doing nothing when you double click on it to start it. I'm going to be discussing in this thread possible reasons these issues are happening, and how to fix them.
Issue 1: MANIFEST.MF
So, the first thing I'm going to be discussing is a problem with the manifest file.
The manifest file, is a file within any supposively runnable .jar file.
It's always in a folder called META-INF and is named MANIFEST.MF.
Naturally, the .MF file extension stands for Manifest File.
Moving on from that, the normal architecture of a manfiest file, you will find, is similar to this:
This is a typical manifest file. As you can see, it has a version, a Class-Path, and a Main-Class section, the Main-Class section is what we are going to be focusing on.
When you attempt to start up your jar file by double click, and nothing happens(javaw.exe), or you get the error(java.exe) "Error: Could not find or load the main class (name).jar", then there may indeed be a problem with your manifest file.
Note, it is only a problem with your manifest file IF you can not run the jar file from command prompt as well, if you can run the jar file from command prompt fine, skip to the next section of this tutorial.
Anyhow, there is several problems the manifest file may have to cause java to not recognize the location of the main class from the manifest file.
Main-Class is set to the incorrect main class
In this issue, the Main-Class of the MANIFEST.MF may be set to the incorrect Main Class file
Say for example, my main class is here in my jar file:
That means that my Main-Class would be convert.Main, since the path to my main class is:
In specifying the main class for the MANIFEST.MF, you must use dots instead of slashes to specify folders.
For example, there is one folder to go into to reach Main.class, that folder is called convert, which is my package, so to reach Main.class, for the Main-Class section of my MAINIFEST.MF, I would have convert.Main.
Under Main-Class, you must also for your class not have .class. Just have it as (folder).(folder).(class)
For example, my working MANIFEST.MF looks like this:
MANIFEST.MF formatted incorrectly
In this issue, you get the error "no main manifest attribute, in (name).jar"
This error is normally caused by inproper formatting of the MANIFEST.MF.
To correctly format a MANIFEST.MF file, you need a Manifest-Version, a Class-Path, and a Main-Class.
As well as this, you need, at the bottom, to have pressed enter twise. The MANIFEST.MF file is case-sensitive.
For example, a working MANIFEST.MF file, would fully look like this:
(Note, normally there wouldn't be any spaces in those bottom 2 slots, but I had to put spaces there for this forum to show them)
As you can see, I have a Manifest-Version, a Class-Path, and a Main-Class, as well as having pressed enter twise.
This is a correctly formatted MANIFEST.MF, and it will not get the error. Change your MANIFEST.MF accordingly.
If your jar file still doesn't work on double clicking it, go onto the next step.
Issue 2: jar file set to the wrong program
In this issue, you, or windows, may have set your .jar file extension to open with a wrong, or non-existant program. To fix this, there are multiple steps.
First, open up control panel. You can open control panel by clicking the start menu icon in the bottom left corner, and clicking control panel.
Next, you need to click on the thing that says Default Programs.
After that, you must click on Associate a file type or protocol with a program
Windows will then load up a list of all known extensions on your computer. What you are looking for is the .jar extension. Scroll down until you find the J's, then look for .jar.
After you find the .jar extension, make sure, first off, that it opens with Java by looking at the top bar. Second off, we are going to make sure it opens with, contrary to popular belief, java.exe. Opening the jarfile with Java.exe will allow us to, if this process doesn't work, get a easy error report.
To do this, click on Change program..., and then you will be presented with a new window
In this window, click browse, and then browse to your java directory. For a standard 64-bit java installation, you would find it here:
In this directory, go to bin, then find java.exe and click on it, then click Open.
Now back in the Choose program window, click Ok, and you're done.
Now attempt to double click your jar file again, if it still does not run correctly on double clicking, go to the next step.
Issue 3: Java has a incorrect entry in the registry
In this issue, most likely due to a reinstall, Java has created a incorrect entry in the windows registry that is stopping your jar file from running correctly.
This error in the registry creates the error "Error: Could not find or load the main class (name).jar" similar to when the MANIFEST.MF file has a incorrect Main-Class.
Alright, if you're reading this far, you decided that you would rather follow my steps on fixing the java registry.
First off, you need to open up the windows registry editor. You can do this by either going into the start menu, which I showed you how to access earlier, and clicking run.
Alternatively, you can press the windows key + the R key to open the run menu.
After you do this, a little box will pop up, in this box, type regedit, and press Enter or the Ok button.
This will open the windows registry editor, hurray!
Anyhow, you need to look for a folder called HKEY_CLASSES_ROOT.
This folder contains the Java registry values which we are looking for.
Anyhow, open this folder by clicking the little white tab, and you will be met with a giant mess of many many folders.
What you need to find is the folder called Applications.
After you find this folder, open it, again, by using the little white tab to the left of it.
You will again be met by many many folders, look for the folder that is labeled java.exe.
This is the folder we are looking for.
Open this folder, as well as all the folders within it that are openable. You will eventually come up with a folder called command.
Click on that folder once.
In the white box to the right, you will see some columns which contain values.
A Name box, a Type box, and a Data box.
If your Data box is different from my Data box, you need to edit it to be like mine.
To edit what is in these boxes, right click on the value under name (Default) and click modify.
You will then be presented with a new window.
Change the Value data to what my Value data is, and then click Ok.
After you do this, exit the registry by clicking the red X, and attempt to double click your .jar file. If all is well, your jar file will open without issue.
If you want error logs to show up for any errors your jar may have during operation, change javaw to java, and error logs will show up in a command prompt box. Alternatively, you can just manually run the jar from command prompt to get error logs using the java -jar command.
Also, make sure you follow issue 2, and that the default program for opening .jar files is java.exe
Leave comments on new problems that occur, and any fixed problems.
I've seen people before make temporary solutions on issues, that is no way to solve something.
Richie Robert wrote: . . .
As well as this, you need, at the bottom, to have pressed enter twise. . . . .
I thought you only had to press enter/return once at the end of the manifest file. If your first quote without the blank line was correct, that manifest file wouldn’t work, but I don’t think the double blank line will make any difference.
In the first screenshot (please post text instead in future) you can see the main class appears to have an unusual spelling; is this in the manifest file?
I am not sure what to suggest, since you have tried lots of things, but there are two other things
1: Create compile and execute a HelloWorld class. That will confirm your Java™ installation
2: Unzip the .jar file, navigate to the folder one “above” the convert folder, and try to execute the app with java convert.Main
... and welcome to the Ranch
Joined: Oct 13, 2005
At the risk of appearing impatient, any luck with it yet?
Thank you so much!!! I had the registry issue, and I had never encountered it before. I was COMPLETELY baffled when my project would run from the command line but not by double clicking. You really saved my bacon man, thanks again!!
Joined: Oct 13, 2005
Shows that old posts can be useful
Joined: Jan 01, 2013
Campbell Ritchie wrote:Shows that old posts can be useful
Precisely! Hate to start a new topic when one already exists, even if it is old.