tags:

views:

36

answers:

5

I know this is a very common question, but I tried googling and got no helpful result. I'm trying to make a jar out of my "Main.class" file (made in NetBeans, but I'm not trying to make the jar with it)

I ran this command:

D:\NetBeans\trovanum3\build\classes\trovanum3>jar cf trovanum.jar *.class

And a .jar file spawned in that folder. I thought that would've worked, but it won't run.

I tried opening the jar file with an archive opener and inside it are the "Main.class" file and a "META-INF" folder containing a file named "MANIFEST.MF" The file contains this:

Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)

What could be the problem?

A: 

You need to specify the Main-Class property in the manifest file: Understanding the Manifest. This usually means you need to add the manifest file yourself instead of letting it be added automatically.

By the way you can always run your code without a manifest like this:

java -cp path/to/myJar.jar my.package.MyClass

The manifest is required when you want to run it like this:

java -jar path/to/myJar.jar
Mark Peters
A: 

Extract the mainifest file.

Add an extra line so it says :

Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: Main

Make sure there is two newline characters at the end.

Update the jar file. You could either have edited the Manifest in the jar file with WinRAR, 7-zip, etc. or you could have deleted the original jar (after extracting and editing) then ran

jar -cmf MANIFEST.MF trovanum.jar *.class

Then it would use your newly modified manifest in the jar.

Leo Izen
I did so, and I'm now getting this error: Could not find the main class: Main. Program will exit.
Gabriele Cirulli
@Gabriele: Replace "Main" in Leo's example with the fully-qualified classname of your Java file that contains the `main()` method that you want to run. E.g. `Main-Class: mypackage.MyClass`
Mark Peters
That's main(String[] args), but yes. I didn't mean Main, i meant insert-name-of-your-main-class-here.
Leo Izen
Mm.. What should I put in exactly? I tried Main-Class: trovanum3.Main, but it didn't work. Here's a screenshot from NetBeans of the package and class names, I think they are the same I put in the manifest. http://bit.ly/b8tAdE
Gabriele Cirulli
@Gabriele: You need to say more than "it didn't work." Communicate with us. What exactly happened? Did your computer catch on fire? Was there an error message of some kind?
Mark Peters
Sorry, I meant the same error came up. I put in the package name.class name but it still wouldn't work. (same could not find main class error)
Gabriele Cirulli
+1  A: 

Create a file called well, anything really, but we'll call it manifest.txt

Open manifest.txt and type:

Main-Class: Main

then to create your jar, type

jar cfm trovanum.jar manifest.txt *.class

And it should run fine.

alpha123
Sorry, it still gives me the error.
Gabriele Cirulli
If main is in a package, then it will have to be package.to.main.Main . Don't include the .class suffix, and replace Main with your actual main class.
Leo Izen
What he said. :-P BTW, just using NetBeans to make the jar is really easy, why don't you want to?
alpha123
A: 

You need to set the Main-Class attribute in the manifest of your jar. See the java tutorial link below for details.

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

kkress
A: 

You can use the -e option of jar to specify the entry point, i.e. the main class. For example:

D:\NetBeans\trovanum3\build\classes> jar cfe trovanum.jar trovanum3.Main trovanum3\*.class

Saves fiddling with the manifest if that's all you need in there. Note the working directory is one up from that quoted in your question. Otherwise the Main class is created at the top-level in the jar, rather than in a directory that reflects the package in which it should reside. jar -tvf should then show something like this:

  0 Thu Oct 21 22:34:30 BST 2010 META-INF/
 80 Thu Oct 21 22:34:30 BST 2010 META-INF/MANIFEST.MF
488 Thu Oct 21 22:18:24 BST 2010 trovanum3/Main.class

-e

Sets entrypoint as the application entry point for stand-alone applications bundled into executable jar file. The use of this option creates or overrides the Main-Class attribute value in the manifest file. This option can be used during creation of jar file or while updating the jar file. This option specifies the application entry point without editing or creating the manifest file.

martin clayton