Californ1a
Californ1a

Reputation: 31

How do I create an executable jar file from my program so I can run it per double-click or from command line?

I am using the program Eclipse to compile my .jar

http://pastebin.com/AY79DbgY

It should only be a console window. I am just starting out on java, so I have no idea what I did wrong.

When I try to run the .jar I compiled in Eclipse, it doesn't run at all, but if I open cmd, and try to run it via there, it prints out this, none of which I understand at all:

Exception in thread "main" java.lang.NoClassDefFoundError: server
Caused by: java.lang.ClassNotFoundException: server
        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: server.  Program will exit.

I just wanted to make a .jar that I could run from any computer without having to use Eclipse. It runs just fine in the console there.

Also, every once in a while, when I terminate the script in the console in Eclipse, I get this error, but otherwise it seems to run just fine:

        Exception in thread "main" java.util.NoSuchElementException: No line found
            at java.util.Scanner.nextLine(Unknown Source)
            at finalproject.main(finalproject.java:432)

So I kinda have no idea what I'm doing at this point.

If my typing seems like I'm young, it's true, I'm only 15.

Upvotes: 2

Views: 1457

Answers (3)

jbx
jbx

Reputation: 22128

In your JAR file you should have a file called MANIFEST.MF (under folder META-INF) This describes various properties of your jar file.

You should edit it and add:

Main-Class: finalproject

I would put your class in a package because having it in the default package invites a little classpath trouble.

You can open the JAR file using winzip or winrar and edit the MANIFEST.MF directly just to test, but in reality you should have it outside together with the classes you are packaging in the JAR so that each time you generate the JAR file you include it with the rest of the classes.

Here is how you include the file with additions to the default manifest when generating the JAR file. http://download.oracle.com/javase/tutorial/deployment/jar/modman.html

Upvotes: 0

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

I don't know what you really did here, so here are some guesses from my glass ball:

  • You are calling your program by java -cp file.jar server, while it would have to be java -cp file.jar finalproject.

    Use the latter command.

  • Your IDE indicates some main class (server) in the Jar Manifest which does not exist, and you are using java -jar file.jar. This would also explain why you can't run it from Eclipse.

    Check your build settings to indicate the right main class.

Upvotes: 0

duffymo
duffymo

Reputation: 308733

You need to add the name of the class that contains the main() method that's executed when you run the JAR in the manifest. Take a look at this for instructions.

Upvotes: 3

Related Questions