Shahab
Shahab

Reputation: 2067

Issue in creating and running a .jar file

I have compiled a java project into a jar but I am having some issues when trying to run it

java -jar XMltoCSV.jar
Error: Could not find or load main class XMLtoCSV.class

I have created a manifest file that contains:

Main-Class: xmltocsv.XMLtoCSV

(there is a newline character in the manifest text file)

The XMLtoCSV.class does contain the main() method. I have been running this code to create the jar file:

jar cvf XMLtoCSV.jar Manifest.txt xmltocsv/*.class

And I know its working because I get:

jar cvfm XMLtoCSV.jar manifest.txt XMLtoCSV.class
added manifest
adding: XMLtoCSV.class(in = 4885) (out= 2492)(deflated 48%)

Why does it give me this error? Thanks in advance for the help

It seems I am also running into another error, what does it mean?:

Exception in thread "main" java.lang.UnsupportedClassVersionError: xmltocsv/XMLt oCSV : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: xmltocsv.XMLtoCSV. Program will exit.

Upvotes: 0

Views: 446

Answers (3)

David Archanjo
David Archanjo

Reputation: 101

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

The simplest MANIFEST file would look like the following:

Manifest-Version: 1.0
Main-Class: com.foo.FOO

Just remember to leave three lines in blank after the last entry.

Hope I have helped.

Upvotes: 0

Joe23
Joe23

Reputation: 5782

Drop the .class in your manifest.

Update: What is the package of your main class? If your Java-File contains a statement such as

package com.google.common.base;

at the top. You need that package in your manifest. In your case that would be:

Main-Class: com.google.common.base.XMLtoCSV

If your class does not have such a package statemnent the error is something else.

(Note that com.google.common.baseis just an example of course.)

Upvotes: 3

Robert
Robert

Reputation: 452

Main-Class: XMLtoCSV.class

it's not right.

You have to specify something like:

Main-Class: com.mydomain.XMLtoCSV

namely the fully qualified name of the class.

See: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Upvotes: 4

Related Questions