Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527

    Unanswered: problem with jar files

    I can't seem to create a fully working jar file and wondered if anyone could help. I have an applet, a few wav files and an jpg image that I want to package up into the jar file. The aim, of course, is to call it from a web page. I tried creating a manifest file and managed to create the jar file but found the web page still seems to be calling the original files because if I delete one of the original wav files then that sound stops working but it should be getting everything from the jar file.

    The manifest was:
    Code:
    Manifest-Version: 1.0
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    Main-Class: TheSwarm_v23.class
    I create the jar using:
    Code:
    jar cfm C:\Java\Game\TheSwarm.jar C:\Java\Game\Manifest.txt C:\Java\Game\*.class C:\Java\Game\*.wav C:\Java\Game\*.jpg
    The applet call is:
    <applet code=TheSwarm_v23.class
    archive="TheSwarm.jar"
    width=120 height=120>
    </applet>

    Any ideas?
    Is it easy to add a splash screen for when the jar is loading - I couldn't get that to work either?

    Cheers Mike

  2. #2
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    I think your main-class is wrong. You're specifying the symbolic class name, as you would in code. So it should be just TheSwarm_v23, assuming that's what your class is called.

    If that's the case, the applet is probably failing to load from the .jar at all; not sure why it is falling back to the individual files.

    Here's a tiny example you can run from a command line that tries to determine the URL for a single resource.

    If you run make.bat, you can then run it with:

    Code:
    ~/tmp/jartest $ java -jar simple.jar 
    jar:file:/Users/ben/tmp/jartest/simple.jar!/simplejar/res.txt
    That's the correct URL as you can see by listing the file:

    Code:
    ~/tmp/jartest $ jar -tf simple.jar
    META-INF/
    META-INF/MANIFEST.MF
    simplejar/hello.class
    simplejar/res.txt
    Note that I put the res.txt in the same directory as the class; that's where getResource looks first for a plain resource.
    Attached Files Attached Files

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    My jar file now contains:
    Code:
    C:\Program Files\Java\jdk1.6.0_14\bin>jar -cvmf C:/Java/Game/Manifest.txt C:/Jav
    a/Game/TheSwarm.jar C:/Java/Game/*.class C:/Java/Game/*.wav C:/Java/Game/*.jpg
    added manifest
    adding: C:/Java/Game/TheSwarm_v23.class(in = 18582) (out= 10453)(deflated 43%)
    adding: C:/Java/Game/alien_fire.wav(in = 20292) (out= 16263)(deflated 19%)
    adding: C:/Java/Game/alien_hit.wav(in = 27148) (out= 23145)(deflated 14%)
    adding: C:/Java/Game/die.wav(in = 54309) (out= 49285)(deflated 9%)
    adding: C:/Java/Game/fire.wav(in = 6742) (out= 5601)(deflated 16%)
    adding: C:/Java/Game/highscore.wav(in = 125164) (out= 107442)(deflated 14%)
    adding: C:/Java/Game/level.wav(in = 110646) (out= 100853)(deflated 8%)
    adding: C:/Java/Game/mother_hit.wav(in = 42904) (out= 26484)(deflated 38%)
    adding: C:/Java/Game/mother_travel.wav(in = 5170) (out= 2008)(deflated 61%)
    adding: C:/Java/Game/shield.wav(in = 51084) (out= 45711)(deflated 10%)
    adding: C:/Java/Game/moon.jpg(in = 5857) (out= 5146)(deflated 12%)
    adding: C:/Java/Game/space.jpg(in = 23401) (out= 23199)(deflated 0%)
    I copied it to the server directory and removed all the individual files that were already there. I was given the following error which indicates it can't find the class file. At least it's looking for the right class file though and the extract above seems to indicate the jar contains this file. Interestingly the manifest file I supplied was called Manifest.txt but above its called MANIFEST.MF - does this mean it's ignoring my manifest file or is it renaming it.

    Code:
    Java Plug-in 1.6.0_17
    Using JRE version 1.6.0_17-b04 Java HotSpot(TM) Client VM
    User home directory = C:\Documents and Settings\Mike Robinson
    ----------------------------------------------------
    c:   clear console window
    f:   finalize objects on finalization queue
    g:   garbage collect
    h:   display this help message
    l:   dump classloader list
    m:   print memory usage
    o:   trigger logging
    q:   hide console
    r:   reload policy configuration
    s:   dump system and deployment properties
    t:   dump thread list
    v:   dump thread stack
    x:   clear classloader cache
    0-5: set trace level to <n>
    ----------------------------------------------------
    
    
    load: class TheSwarm_v23.class not found.
    java.lang.ClassNotFoundException: TheSwarm_v23.class
    	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable****n(Unknown Source)
    	at java.lang.Thread****n(Unknown Source)
    Caused by: java.io.IOException: open HTTP connection failed:http://www.checkmypages.com/swarm/TheSwarm_v23/class.class
    	at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader$1****n(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	... 7 more
    Exception: java.lang.ClassNotFoundException: TheSwarm_v23.class
    It looks like it's just calling the individual files as normal and ignoring the jar file all together.

  4. #4
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    Your manifest should definitely say 'TheSwarm_v23', not 'TheSwarm_v23.class', same for the applet code parameter.

    The main class entry in the manifest file is the class object, not the .class file.

    You might have some other small glitches. It took me a couple of goes to get that small example right because Java is so damned finicky. You could actually add that main method to your applet and try running it on the command line with java -jar to help see what's going on.

    Also, run jar -tf TheSwarm.jar to check the jar's directory structure after you've created it. jar -cv just shows what file it's reading, not what directory entry it's creating in the jar.
    Last edited by sco08y; 01-10-10 at 11:12.

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Cheers sco08y

    I was building the jar file on my local PC and then transfering it to my server. Using the command above it showed that it was expecting all the files to be in the directory C:/Java/... which was definitely not the case on my Unix box!

    I solved this by just building the jar file on the Unix box and everything ran fine. Thanks for your help.

    Can the JAR file be run outside of a browser ie as an application?

    I tried including this main method in the applet class and then double clicked the jar file (in XP) and it said "Could not find the main class: TheSwarm_v23". It's not clear what I'm missing.
    Code:
    	public static void main(String[] args) {
    		//... Create an initialize the applet.
    		TheSwarm_v23 theApplet = new TheSwarm_v23();
    		theApplet.init();         // Needed if overridden in applet
    		JFrame window = new JFrame("Sample Applet and Application");
    		window.setContentPane(theApplet);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.pack();              // Arrange the components.
    		window.setVisible(true);    // Make the window visible.
             }

  6. #6
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    Yeah, Java is pretty good about being x-platform, but there are occasional Unixisms.

    If it has a main class, java -jar foo.jar will run a jar, and double-clicky does the same thing but without a console.

    The applet can be run independently with 'appletviewer' or something similar which should be in your Java binary directory.

    "Could not find the main class: TheSwarm_v23"

    You've got the signature right for your main method. So check the package, the class name and use jar -tf foo.jar to see that they match.

Posting Permissions

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