6 Replies Latest reply on Jan 14, 2006 8:03 AM by 843802

    problems with make a jar file runnable

    843802
      Hi there,
      I had a problems when making a jar file runnable.

      I packaged a jar file like this:

      jar cvfm aj.jar Manifest.mf MainPackage/*.class

      and the 'Manifest.mf' file is as follows:
      Manifest-Version: 1.0
      Created-By: 1.5.0_01 (Sun Microsystems Inc.)
      Main-Class: MainPackage.AutoGui


      When I run it with:
      java -jar aj.jar

      I got this error message:
      Exception in thread "main" java.lang.NoClassDefFoundError: 
      hcslib/EasyComponents/MnemonicTabbedPane
              at MainPackage.AutoGui.main(AutoGui.java:35)


      It seems it can not find the class: 'hcslib/EasyComponents/MnemonicTabbedPane' '
      which is in a jar file 'hcslib.jar' even though this jar file is included in the classpath.

      Then I add the Class-Path attribute to the manifest file like this:
      Manifest-Version: 1.0
      Created-By: 1.5.0_01 (Sun Microsystems Inc.)
      Main-Class: MainPackage.AutoGui
      Class-Path: C:\jdk\jre\lib\ext\hcslib.jar


      Now I got another error:
      Exception in thread "main" java.lang.NoClassDefFoundError: 
      MainPackage/AutoGui



      what's wrong with this?

      Any help will be highly appreciated!
        • 1. Re: problems with make a jar file runnable
          843802
          The last error you posted indicates that the folder MainPackage has not been included in the JAR file. Run
          jar -cfm aj.jar MANIFEST.MF MainPackage/
          This will include the whole directory, not just the class files in it.
          • 2. Re: problems with make a jar file runnable
            800560
            When you use java -jar, the only classpath that is used is the path(s) in the jar manifest Class-Path. In your first scenario, you did not have any Class-Path, that's why the JVM could not find the hcslib.jar jar file.

            When you put one or more paths into the Class-Path attribute, the JVM only looks at the specified paths for classes. Since you had only the hcslib.jar in the Class-Path, the JVM could not find MainPackage.AutoGui.

            I am fairly certain changing your Class-Path as shown below will work
             Class-Path: . C:\jdk\jre\lib\ext\hcslib.jar
            Notice it is a space, a period and another space before the path to hcslib.jar.
            • 3. Re: problems with make a jar file runnable
              843802
              hi , Alexander and atmguy ,

              Thank you very much for your reply!

              I tried what you suggested but I still got the same errors. Actually, I tried even more for the Class-Path, like put the aj.jar I want to run to the path list, but it did not work.

              Anyway, I'll give you each a Duke point.
              • 4. Re: problems with make a jar file runnable
                800560
                What happens when you do

                jar tf aj.jar

                You should see: MainPackage/AutoGui.class

                That is, a directory named MainPackage and a class name MainPackage/AutoGui.class
                • 5. Re: problems with make a jar file runnable
                  843802
                  hi atmguy ,

                  Thank you for your reply again!

                  Yes. I can see 'MainPackage/AutoGui.class' when I run jar tf aj.jar.

                  When not packaged, this program works without any problem.

                  For a program that do not reference external libraries, it works well. For the program that do reference external libraries, it cannot find the library class. However, if I add Class-Path, it can not even find the Main Class.

                  Does it mean Main-Class and Class-Path attribute can not be be used in the same manifest file?
                  • 6. Re: problems with make a jar file runnable
                    843802
                    Hi, Alexander and atmguy,

                    The problem has been partly resoved by Jos in another thread:

                    http://forum.java.sun.com/thread.jspa?threadID=698361&messageID=4053882#4053882