0

http://pastebin.com/1btVw8Cb

There are two classes in the above code.

So above is my code which is working fine when I hit run in Eclipse, runs fine in Netbeans as well.

I am trying to create a standalone application, a jar file.

The error I get when I double my jar is:

Could not find the main class: NewJFrame. Program will exit. I get the following from the command promt:

E:\Java Programs\Eclipse Workspace\test3\src\test3>java testT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: testT/jar
Caused by: java.lang.ClassNotFoundException: testT.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: testT.jar.  Program will exit.

I followed the command from here: "Could not find the main class: XX. Program will exit." So this is what I am typing in the command promt to create my jar:

E:\Java Programs\Eclipse Workspace\test3\src\test3>jar cfm MyJar.jar manifest.tx
t *.class SINGLE.TXT

https://i.stack.imgur.com/ncm1q.jpg

Some shots above I took to show the process.

So I think the problem could be error when I do javac? But it builds fine and runs fine in netbeans and eclipse >< Please help.

Community
  • 1
  • 1
Noman Arain
  • 1,172
  • 4
  • 19
  • 45

3 Answers3

2

You need to use the -jar flag.

java -jar MyJar.jar

http://docs.oracle.com/javase/tutorial/deployment/jar/run.html says

JAR Files as Applications

You can run JAR-packaged applications with the Java interpreter. The basic command is:

java -jar jar-file

The -jar flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.

and http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html explains how to put a manifest in the jar.

The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • that did not work. I got the following: http://pastebin.com/npm3ksNd Also, I want this app to be able to run by just double clicking it, because I plan to turn it into an applet. – Noman Arain Mar 17 '12 at 14:57
  • are you sure your main class is within the test3 package and is called NewJFrame? – Jarle Hansen Mar 17 '12 at 15:18
  • yes, if you look at my screenshots, you'll see that NewJFram is there and if you follow the pastebin's link you will see the main method there. – Noman Arain Mar 17 '12 at 15:31
  • @NomanArain, the name of your class in the `Main-Class` manifest entry should be `test3.NewJFrame` not `test3/NewJFrame`. – Mike Samuel Mar 17 '12 at 15:54
  • Mike I changed my mainifest to say Main-Class: test3.NewJFrame but it still gives me the same error, main cannot be found. Error: Could not find the main class: test3.NewJFrame. Program will exit. – Noman Arain Mar 17 '12 at 16:10
  • @NomanArain, Please extract your manifest via `jar xf MyJar.jar META-INF/MANIFEST.MF` and post its content in your question so we can help debug. – Mike Samuel Mar 17 '12 at 16:13
  • I have taken the code out of package, http://pastebin.com/pSrjVCeL this has the code and at the boom i pasted the manifest.mf – Noman Arain Mar 17 '12 at 16:27
  • @NomanArain, In your manifest, you have the line `Main-Class: NewJFrame`. That is missing a package name. – Mike Samuel Mar 17 '12 at 16:33
  • Mike, I took out the package from my code. So my programmed is not packaged anymore. – Noman Arain Mar 17 '12 at 16:51
  • Your screenshot shows CODE not .class files. You have to add the compiled files to the JAR, the source won't do you any good. – Jochen Mar 17 '12 at 18:46
  • @Jochen I am running this command: jar cfm MyJar.jar manifest.tx t *.class SINGLE.TXT, so it's only adding .class classes. – Noman Arain Mar 17 '12 at 20:48
  • Manifest-Version: 1.0 Created-By: 1.6.0_31 (Sun Microsystems Inc.) Main-Class: NewJFrame – Noman Arain Mar 18 '12 at 05:33
0

Add

Main-class: Splash

part to Manifest file. set Main class name instead Splash

That will work...

HackerGK
  • 360
  • 3
  • 15
0

it is now working. I have not changed anything, maybe the memory got curroupted and that is why it was giving me problems. I think restart the computer is what fixed it. But yea, I don't know for sure what was wrong, though it is working fine now. So thank you for your help guys

Noman Arain
  • 1,172
  • 4
  • 19
  • 45