Results 1 to 18 of 18
  1. #1
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default "Cannot find main class" error when creating a .jar!

    Hello. I have a class called GUI.java and (of course) it contains a main method and I would like to make it a jar file for execution. I've tried 100 times with different options, tried using eclipse to do it for me, tried using the command line and making my own manifest file, everything. Whenever I double click the jar I created, it says "Cannot find main class: GUI. Program will now exit". What can I do??

  2. #2
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    bump.. no responses yet??

  3. #3
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    Hi, have you tried calling the Jar from command line?

    Java Code:
    java -jar FileName.jar

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: "Cannot find main class" error when creating a .jar!

    When you export your project as a jar in the last step it asks you to specify the main class.
    Do that!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    Check that the jar file contains a manifest file with a Main-Class: entry that has the correct path to your class.

  6. #6
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    I specified the Main class in Eclipse and my manifest file (from when I was trying to create the jar myself) contained Main-Class: GUI with two carriage returns (enter key) afterwards.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    Is the correctly formatted manifest file in the jar file?

  8. #8
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    Yes it is in the jar file when I open it with 7-zip.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    If the manifest is in the jar and is in correct format, then the java command should start the class named by Main-Class.
    If it does not, then something is wrong and you need to check again.

    Open a command prompt, change to the folder with the jar file and enter:
    java -jar <YOURJARFILENAMEHERE>.jar
    Copy the console contents here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  10. #10
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    OP,You can try this.
    Quote Originally Posted by Norm View Post
    If the manifest is in the jar and is in correct format, then the java command should start the class named by Main-Class.
    If it does not, then something is wrong and you need to check again.

    Open a command prompt, change to the folder with the jar file and enter:
    java -jar <YOURJARFILENAMEHERE>.jar
    Copy the console contents here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Or, you can try this.
    JAVA [creating Jar files] Me, when programming

    As you have created JAR file you should just try the
    Ways to update a jar file with manifest……
    portion.
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  11. #11
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    The strangest thing: when I opened a command window, went to my directory and typed java -jar G.jar (G.jar is the name of my jar file), it ran my jar! But whenever I double click it I get the "Cannot find Main Class GUI.java"... Anything?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    I get the "Cannot find Main Class GUI.java".
    That message is different from the one you posted earlier.
    "Cannot find main class: GUI. Program will now exit".
    Are you sure it is complete and correct?

    Can you look in the Windows registry and see what the command line is for the jar files?

  13. #13
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    Quote Originally Posted by Norm View Post
    That message is different from the one you posted earlier.

    Are you sure it is complete and correct?

    Can you look in the Windows registry and see what the command line is for the jar files?
    @OP
    I have faced the same problem. I use XP,Windows7 and ubuntu. My jar files works on xp and ubuntu fine. But in W7 I got the message "Cannot find Main Class". Try your JAR files on another pc. And You can change OS's default file type.Make sure that when double clicking It is opened by javaw.exe
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    I have several different versions of java on my machine. I get different results when I try to open a jar file that does not have a manifest file with different versions of java.
    One gives me this error window:
    Attached Thumbnails Attached Thumbnails &quot;Cannot find main class&quot; error when creating a .jar!-jvm_errormsg.png  

  15. #15
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    The first error I posted was correct. I wrote the second one from memory, sorry. I'm running windows 7 x64 so this must be the issue. Thanks everyone

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    What is the full text of the error message you get?
    Here's what I get when I leave out the -jar option:

    Microsoft Windows XP [Version 5.1.2600]

    D:\JavaDevelopment>java acme.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: acme/jar
    Caused by: java.lang.ClassNotFoundException: acme.jar
    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: acme.jar. Program will exit.

  17. #17
    davetheant is offline Member
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default Re: "Cannot find main class" error when creating a .jar!

    It's nothing like that. A window comes up saying "Cannot find main class: GUI. Program will now exit" and that's all. It works from the command line and works on my windows vista desktop.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: "Cannot find main class" error when creating a .jar!

    Strange that the message wording is different:
    Could not vs Cannot
    will exit vs will now exit

    Can you screen print the message window and post it here?

Similar Threads

  1. Replies: 11
    Last Post: 08-18-2011, 10:54 PM
  2. Receiving error "Could not find the main class"
    By Kalpesh in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 06:31 PM
  3. Error message "could not find the main class"
    By srwpchelp in forum New To Java
    Replies: 8
    Last Post: 12-23-2010, 09:15 PM
  4. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •