2

I'm trying to create a jar with the jar tool. Using the following command

jar.exe cmfv manifest.txt lol.jar Main.class

This generates a jar with the following manifest:

Manifest-Version: 1.0
Created-By: 1.7.0_03 (Oracle Corporation)
Main-Class: Main

When I run the jar from command line (java -jar lol.jar) it runs fine. But when I double click the jar in my folder it gives an error: "Could not find the main class: Main.Program will exit."

What could be causing this?


After trying some stuff out the Manifest currently looks like this:

Manifest-Version: 1.0
Class-Path: .
Created-By: 1.7.0_03 (Oracle Corporation)
Main-Class: code.Main

The Main class has a package declaration added. Inside the jar the 'code' folder/package is added. Still have the same error though.

Reinard
  • 3,624
  • 10
  • 43
  • 61

3 Answers3

1

You should put your Main class into a package, and adjust your manifest correspondingly. That should solve the problem

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Added a package declaration to Main.class and moved it to a corresponding path. Still same error though. – Reinard May 31 '12 at 18:08
  • Main-Class: code.Main and the Main.class is inside a code folder inside the jar – Reinard May 31 '12 at 18:11
  • Is "code" your package? Main.class needs to be in a sub directory tree that matches its package. If it is, it is possible you are not creating your jar correctly – ControlAltDel May 31 '12 at 18:17
  • Was the class rebuilt after changing package declration? I'm not sure if the jar utility re-builds the classes. If not, sometimes i have to do a clean/build to get it to overwrite the .class files. – aglassman May 31 '12 at 18:20
0

You need to put the Main class in your classpath. When you run it from the command line, your current directory is auto-added to your classpath, this is why it works from there. When you double click it, you are not specifying the path to the jar as in the classpath. There are ways to add the class to your classpath in your manifest. Below is an example of one of my jars. lib is a folder within the jar, com/sample/CommandLineClient.class is my main class.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.5.0_12-b04 (Sun Microsystems Inc.)
Class-Path: lib/args4j-2.0.19.jar lib/axis.jar lib/axis-ant.jar lib/commons-discovery-0.2.jar lib/commons-logging-1.0.4.jar lib/jaxrpc.jar lib/log4j-1.2.8.jar lib/saaj.jar lib/wsdl4j-1.5.1.jar
Main-Class: com.sample.CommandLineClientSysIn
aglassman
  • 2,643
  • 1
  • 17
  • 30
  • I'm not using any additional jars or classes (apart from some in java.io). So this shouldn't be necessary? – Reinard May 31 '12 at 18:17
0

What is the file association with the .jar extensions?

Double click the jar file should make the jar run woth the javaw command.

Try making jar files to be executed by default with javaw.

Also take a look at this: http://www.wikihow.com/Run-a-.Jar-Java-File

lcguida
  • 3,787
  • 2
  • 33
  • 56