Results 1 to 6 of 6
  1. #1
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default jar file won't execute.

    Hi all,

    I have another minimal example with which I have failed to produce any results. My test application is a very simple application.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class TestApp
    {
    	public static void main(String[] args)
    	{
    		javax.swing.SwingUtilities.invokeLater(new Runnable()
    		{
                public void run()
    			{
    				System.out.println("This is a test.");
                }
      		});
    	}
    }
    Now this compiles ok into a .class file which I then add to a jar file but try as I might I cannot get the jar file to run. This should be so simple and yet it turns out to be so hard for me.

    Here is my attempts below:

    Java Code:
    C:\Projects\Java>cd minimal
    
    C:\Projects\Java\minimal>javac TestApp.java
    
    C:\Projects\Java\minimal>java TestApp
    This is a test.
    
    C:\Projects\Java\minimal>
    C:\Projects\Java\minimal>
    C:\Projects\Java\minimal>
    C:\Projects\Java\minimal>jar cf TestApp.jar TestApp.class
    
    C:\Projects\Java\minimal>java -jar TestApp.jar
    Failed to load Main-Class manifest attribute from
    TestApp.jar
    
    C:\Projects\Java\minimal>
    
    C:\Projects\Java\minimal>jar tf TestApp.jar
    META-INF/
    META-INF/MANIFEST.MF
    TestApp.class
    
    C:\Projects\Java\minimal>jar xf TestApp.jar META-INF\MANIFEST.MF
    
    C:\Projects\Java\minimal>cd META-INF
    
    C:\Projects\Java\minimal\META-INF>type MANIFEST.MF
    Manifest-Version: 1.0
    Created-By: 1.6.0_16 (Sun Microsystems Inc.)
    
    
    C:\Projects\Java\minimal\META-INF>
    
    
    C:\Projects\Java\minimal>jar tf TestApp.jar
    META-INF/
    TestApp.class
    
    C:\Projects\Java\minimal>jar ufm TestApp.jar META-INF\MANIFEST.MF
    
    C:\Projects\Java\minimal>jar tf TestApp.jar
    META-INF/
    TestApp.class
    META-INF/MANIFEST.MF
    
    C:\Projects\Java\minimal>java -jar TestApp.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: TestApp$1
            at TestApp.main(TestApp.java:12)
    Caused by: java.lang.ClassNotFoundException: TestApp$1
            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)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            ... 1 more
    
    C:\Projects\Java\minimal>
    As you can see I have tried to update the Manifest file with the start class but no success. I seemed to actually get the right class to run, as you can see from the error above, but it still fails to execute inside the main method.


    My manifest file at last inspection contains the following lines:

    Java Code:
    C:\Projects\Java\minimal>jar xf TestApp.jar META-INF\MANIFEST.MF
    
    C:\Projects\Java\minimal>cd META-INF
    
    C:\Projects\Java\minimal\META-INF>dir
     Volume in drive C has no label.
     Volume Serial Number is 8872-8339
    
     Directory of C:\Projects\Java\minimal\META-INF
    
    22/10/2009  11:48 a.m.    <DIR>          .
    22/10/2009  11:48 a.m.    <DIR>          ..
    22/10/2009  11:40 a.m.                92 MANIFEST.MF
                   1 File(s)             92 bytes
                   2 Dir(s)  22,825,287,680 bytes free
    
    C:\Projects\Java\minimal\META-INF>type MANIFEST.MF
    Manifest-Version: 1.0
    Created-By: 1.6.0_16 (Sun Microsystems Inc.)
    Main-Class: TestApp
    Any ideas?

    Thanks in advance.

    Glenn (Splat).

  2. #2
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    Your class does not have a package name. Give it a package name and make sure the class file is located in a directory structure similar to the package.

    For example: the package "me.classes" should mean that Test.class is in a directory structure looking like "./me/classes/Text.class"

    then jar up your class from ./ (so the directory structure is included.)

  3. #3
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    Ok, I've added in the package name and recompiled and now it will execute from the directory above prefixed by the package name. Have also changed the MANIFEST file as you can see below. This is the jar xf extracted MANIFEST file as well.

    Java Code:
    C:\Projects\Java\META-INF>type MANIFEST.MF
    Manifest-Version: 1.0
    Created-By: 1.6.0_16 (Sun Microsystems Inc.)
    Main-Class: minimal.TestApp
    
    
    C:\Projects\Java\META-INF>cd ..
    
    C:\Projects\Java>java minimal.TestApp
    This is a test.
    
    C:\Projects\Java>java -jar TestApp.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: minimal/TestApp$1
            at minimal.TestApp.main(TestApp.java:13)
    Caused by: java.lang.ClassNotFoundException: minimal.TestApp$1
            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)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            ... 1 more
    
    C:\Projects\Java>
    The error occurs on this line:
    Java Code:
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    which to me implies that the main method is at least getting called.

    Any ideas?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you define the Main-Class attribute in Manifest file, which defines the entry point of your application(jar file)?

  5. #5
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    I got it to work. Not sure how I did it now. I think I found it easiest to just delete the jar archive and start again. Compiling from outside the top level directory did seem to help in the end though. Basically trying to modify the MANIFEST.MF file once it has been created seems to be a bad idea. using the jar -cvfm jarfile.jar manifest inputfile(s) method seems to be a better idea.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, that 'm' have an advantage(sometimes). It take the information from the existing manifest file. But if you've added wrong data in initial file it can cause lots of issues for you. Best thing is that delete those such newly created stuff before redo the process :)

Similar Threads

  1. Execute() throws an NPE
    By mjz in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 02:25 AM
  2. Execute external file/program
    By sirwiggles in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 11:35 PM
  3. execute on server and read from file
    By futurpc in forum Networking
    Replies: 1
    Last Post: 03-13-2009, 07:45 PM
  4. Execute Jar file
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:13 AM
  5. why does not execute this?
    By aleplgr in forum New To Java
    Replies: 6
    Last Post: 08-01-2007, 11:37 AM

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
  •