Mel San
Mel San

Reputation: 93

Jar executable can't find main class - weird one

Hello I have a strange problem. I'm packaging an application with eclipse to produce an executable jar file and when I run it I get the error "Error cant find main class [...]".

When I run the same package from the command line example

java -jar app.jar

The program launches.

What is weird is that the error occurs only with applications complied in Java 7 enviroment.

If I set project to java 6 everything works fine.

What should I do to run Java 7 jar executable?

Thanks for any help.

Upvotes: 4

Views: 5892

Answers (4)

thejartender
thejartender

Reputation: 9379

By default a jar is not executable

Java archives are more often used as libraries where the jar format compresses the code into a single folder. Such code is usually not run as an application (standalone), but there is a provision to turn a jar file into an executable application through means of the Manifest file

Simply create a Manifest file indicating which class contains the 'main' method, to make your jar executable and include the Manifest in your .jar

Upvotes: 0

Muhammad Imran Tariq
Muhammad Imran Tariq

Reputation: 23352

You need to set main class in manifest file

Sample manifest file

Manifest-Version: 1.0
Created-By: 1.3.1 (Sun Microsystems, Inc.)
Main-Class: com.package.MainClassName
Class-Path: oc4j.jar

Make sure that you have set all these things like in sample jar file

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30638

Check the version of your jre if its jre6 then it may be a problem try to update your jre to jre7 it coud do a trick..

Upvotes: 1

Akash Yadav
Akash Yadav

Reputation: 2421

This issue occurs when the JVM is not able to locate the main class associated with the Jar file, JVM uses Main-Class attribute from Manifest.mf to identify the main class associated

Seems like you have missed to Add Main-Class Attribute to the Manifest File that was used to create the JAr ,http://www.skylit.com/javamethods/faqs/createjar.html might help

Upvotes: 0

Related Questions