1 2 3 Previous Next 39 Replies Latest reply on Mar 10, 2009 2:38 PM by PhHein

    Could not find Main Class while executing JAR

    843802
      Hello all,

      I am having a simple Jar file that contains one java class that merely displays a window. I am using NetBeans IDE to create the JAR file. When i execute this JAR file by double clicking on the file, it works fine on the machine. However, if i try to execute this same file on any other windows computer, it gives an error "Could not find the main class. Program will exit."

      In other words, the JAR file is only working on the computer that has NetBeans and JDK installed on. Not on any other machine. Although the other machines have the JAVA Virtual Machine installed.

      I would appreciated any help on this topic.
        • 1. Re: Could not find Main Class while executing JAR
          843802
          if you are running the jar file using
          java -jar <jar-file>
          you need to have the Main-Class attribute in the Manifest file

          have a look at
          http://java.sun.com/developer/Books/javaprogramming/JAR/basics/run.html
          • 2. Re: Could not find Main Class while executing JAR
            843802
            I have a similar problem. Let me explain the steps I took:

            - I've downloaded Netbeans 4.0 Beta 2.
            - I've created a standard application project called: Maintest
            - The project is the default project, creating a main class called: Main
            - I've compiled and ran the "do-nothing" application.
            So far so good.
            - The Manifest.mf file is created automatically and placed inside the jar file.
            So far so goo.
            - I've tried to run the jar outside the ide (Win XP environment).
            The error occurs.
            - I've unpacked the jar file and checked out the included Manifest:
            Manifest-Version: 1.0
            Ant-Version: Apache Ant 1.6.2
            Created-By: 1.5.0-b64 (Sun Microsystems Inc.)
            Main-Class: maintest.Main
            X-COMMENT: Main-Class will be added automatically by build

            So far so good.

            Yet the error remains. What could possibly be wrong?
            Shall I upload this minimalistic jar file for you guys to download and check out? No problem if you would like to see.
            • 3. Re: Could not find Main Class while executing JAR
              843802
              Please send it to me, hussam@kfupm.edu.sa

              I am expecting a problem while jaring the file. I believe that the files Jarred are jarred using absulote file paths which would create such a problem, try to jar it using relative file paths.
              • 4. Re: Could not find Main Class while executing JAR
                843802
                I have the exactly the same problem, please someone post a solution
                • 5. Re: Could not find Main Class while executing JAR
                  843802
                  I am also having this exact same error! please help!
                  • 6. Re: Could not find Main Class while executing JAR
                    843802
                    I'm facing the same prob too.. it has been so long.. someone please help....
                    • 7. Re: Could not find Main Class while executing JAR
                      843802
                      I'm facing the same prob too... could it be an Ant problem?
                      It also puts an error symbol on the jar file, and does not provide informations about this error.
                      Thanks
                      Agostino
                      • 8. Re: Could not find Main Class while executing JAR
                        843802
                        I had the same problem (i.e. Message-Box with "Could not find the main class. Program will exit.")
                        After insuring that the manifest contained a Main-Class: argument and that the argument pointed to the right class, I started the jar via
                        java -jar myjar.jar
                        and read the stacktrace, which is much better than the popup-window.

                        In my case the error was, that the Main-Class could not be loaded due to another referenced class, which was not on the classpath. (i.e. it was not in the same jar-File and the manifest didn't include a Class-Path: argument.)
                        After adding the correct Class-Path: argument in the manifest, the problem went away.

                        I now have the following setup:
                        myjar.jar and somethirdparty.jar in the same directory.
                        myjar.jar contains a manifest which includes the lines
                        Class-Path: somethirdparty.jar
                        Main-Class: my.package.and.MainClass
                        after the normal Manifest-header.
                        • 9. Re: Could not find Main Class while executing JAR
                          843802
                          And I have the very same problem

                          Actually I had this archive working with version 1.01 of my program. After that I made a lot of changes and the version 1.02 jar file can't find it's main class! I Have all depencies, manifest and classes in JBuilder2005 generated jar file.

                          I did work, it doesn't work now, when I'd changed the code!
                          I use JBuilder2005, WinXP what can be the problem??
                          • 10. Re: Could not find Main Class while executing JAR
                            843802
                            have created the jar file in jbuilder.
                            the excutable jar will be excuted iff if it can find jre/lib and jre\bin
                            for instance place the jar file at jre folder exist in jdk.
                            u cant run it even if u have all dependencies and all class files
                            do this , write a bat file with this command
                            "jre\bin\javaw.exe" -jar "yourfile.jar" .
                            place your bat file and jar file in jre directory.
                            or place jar in jre dir and excute it.
                            • 11. Re: Could not find Main Class while executing JAR
                              843802
                              I've had a similar problem.
                              A simple hello world form would run fine in the IDE but not from a JAR file. It turned out that I used the AbsoluteLayout manager which is a Netbeans component and not native Java. As I had not included these files in the Jar build they could not be found. To fix this problem you can add them to your project files so they will be added in the JAr build. To do this:

                              Copy these files:
                              AbsoluteLayout.java
                              AbsoluteConstraints.java

                              From this location:
                              C:\Program Files\netbeans-4.0\ide4\sources\org\netbeans\lib\awtextra

                              to your project directory.
                              build the project (F5)
                              then build the Jar
                              (shift F11)

                              double clicking the Jar file now runs the app.

                              Does this help anybody?
                              • 12. Re: Could not find Main Class while executing JAR
                                843802
                                copied those files into the source directory, compiled etc. like you said, but it didn't do anything for me... i'm having the same problem as everyone else. for some of them i got it to work by specifying the manifest file using

                                jar mcf mainClass file.jar *.class

                                where file.jar is the jar you want to create and of course the class files you want to jar are in the current directory.

                                and then the followin text in the mainClass file:

                                Main-Class: theClass

                                this might help some of you but but this doesn't work for some of the GUI projects i'm doing when exporting over to other operating systems, diff versions of java environment etc for some reason.
                                • 13. Re: Could not find Main Class while executing JAR
                                  843802
                                  works fine on the machine. However, if i try to
                                  execute this same file on any other windows computer,
                                  it gives an error "Could not find the main class.
                                  Program will exit."
                                  yProwler - I think I figured it out.. i had the same problem as you did originally. worked fine on my computer but nothing on any other.. It's because we're running the latest version of Java! (duh). I finally installed an older JDK (1.3) and then compiled etc. by changing the target in Netbeans (right click on the project node in the projects window and go to properties to choose which JDK you want to compile with). Of course I got errors because some of the functions are different etc. finally got it to compile and run, ran it on some other windows machines and it worked! GUI and all... of course the GUI looks different because it's the older version, but the functionality is there.
                                  • 14. Re: Could not find Main Class while executing JAR
                                    843802
                                    Okay, so are you saying that they could update their java version to run the program? If so, what's the link for the correct download (for clients, not developers)? Is there a way to check their computers for the correct version and, if they don't have it, to give them a link?

                                    Thanks for the help. (That has been really annoying.)
                                    1 2 3 Previous Next