Java: Could not find main class. Program will exit.

In order to make a runnable (executable) JAR file which can be started by double clicking the JAR file, you just need to add a manifest file to the JAR file which references the main class to be executed. To do this, just create a file called manifest.txt and add the following contents:

Main-Class: com.benohead.app.Main

Replace com.benohead.app.Main by the fully qualified name of your main class (the class containing the static main method to be executed). Make sure that there is a newline after the fully qualified name since it could make problems later on if it is missing. You can then make the JAR file using your class files and the manifest file:

jar -cvfm MyJarFile.jar manifest.txt com/benohead/app/*.class

A manifest.mf file created and added instead of the manifest.txt file. When you double click on the create JAR file, the main class will be automatically executed. Now, you may be able to run the JAR file like this on some machine but it might also fail on other machines, with the following error message:

Could not find main class. Program will exit.

You may also notice that the following works on some machines where the double click doesn’t work:

java -jar MyJarFile.jar

The problem is probably that you compiled your code with a new version of the JDK (e.g. JDK 6) but an old version of JRE (e.g. JRE 5) is used on double click. You may wonder why it is a problem when you double click but not when you run it from the command line. The answer is that you might be using different versions on the JRE when double clicking and when call java from the command line. To check this, you can execute the following two commands. To check the version used on double click:

ftype | find "jarfile"

It will return something like this:

jarfile=”C:\Program Files\Java\jre1.5.0_14\bin\javaw.exe” -jar “%1” %*

This shows that JRE 5 is used on double click. If you compiled your code using JDK 6 it will be a problem. To check the version used from the command line:

java -version

It will return something like this:

java version “1.6.0_20” Java(TM) SE Runtime Environment (build 1.6.0_20-b02) Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode)

In this case it is using JRE 6 which is the reason why it worked from the command line. So how do you fix it ? There are two ways to do it:

  1. Reinstall the new JRE. It should then fix the file association in the OS
  2. Fix the file association manually

For the second one, you can execute the following in a command prompt:

ftype jarfile="C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1" %*

Of course you need to find the path to the new JRE and use it instead of the path above. Depending on your operating you can use the command below to find it:

where javaw

(where is some kind of Windows equivalent to the which command under Linux and is available from Windows 2003 Server onwards, so including Windows Vista, Windows 7 and Windows 8) Note that to check whether the problem is really a java version issue, you can also start the JAR file from the command line using the same version as used in the double click scenario e.g.:

"C:\Program Files\Java\jre1.5.0_14\bin\javaw.exe" -jar MyJarFile.jar

If you have a version issue, you should get an exception along the lines:

“Exception in thread “main” java.lang.UnsupportedClassVersionError: com/benohead/app/Main (Unsupported major.minor version 50.0)”).

Here’s for your reference a mapping of java versions to major.minor versions:

Java Version Major Version
Java 1.1          45.0
Java 1.2          46.0
Java 1.3          47.0
Java 1.4          48.0
Java 5             49.0
java 6             50.0
java 7             51.0
java 8             52.0

One thought on “Java: Could not find main class. Program will exit.

Leave a Reply

Your email address will not be published. Required fields are marked *