Results 1 to 13 of 13
  1. #1
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default Serious Jar-file problem

    Greetings!

    (Dictionary: sjakk = chess)

    I've run into problems before when trying to create a jar-file
    The issue is discussed here:
    Got problem with jar-file, any help?

    This is the current problem:
    At my College we've had a larger java-project, and it is expected that we will deliver a runable file (in our case a jar-file). We're programming chess, yay.

    This involves a lot of classes on this one, but Main.class in the main one.

    This is the manifest.txt:
    Main-Class: Main

    All classes share this in common:
    package Sjakk;
    This is a issue that was not relevant last time (see link above).

    This is the command:
    jar cvfm Sjakk.jar manifest.txt *.class

    This is what happens when I try to run from Finder:
    A message pops up, telling me that "The Java JAR-file "Sjakk.jar" could not be started. Look after error message in the terminal" (translated by me)

    This is what happens when I try to run this command:
    java Sjakk.jar
    Result:
    Exception in thread "main" java.lang.NoClassDefFoundError: Sjakk/jar
    Caused by: java.lang.ClassNotFoundException: Sjakk.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)

    I haven't tested this out in Windows for the response, yet, but my friend got the message on his computer that the main class could not be found. He typed the exact same command as I.

    Interesting side note: If I go to the parent folder, and type "java Sjakk.Main", the Main-class runs as it should.

    When running the jar-file generator in NetBeans, we get a program that begins the prosess, but it never continues (the prosess exist, but nothing happens). Testing shows that this method works with more simple codes/project, so I assume this is due to the coding in this case. I assume there is an exception that is thrown, and going into some sort of loop. I don't know if this is relevant to the issue above, since this includes other types of files as well.
    Yes, we used NetBeans for this project.

    I need response quick. This project is to be handed in tomorrow (current
    local time: 6:40 pm).

    Thanks! :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Igbear View Post
    jar cvfm Sjakk.jar manifest.txt *.class
    Make that:

    Java Code:
    jar cvmf Sjakk.jar manifest.txt *.class
    Quote Originally Posted by Igbear View Post
    java Sjakk.jar
    Make that:

    Java Code:
    java -jar Sjakk.jar
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Funny how the order of those four letters make a difference.

    This is what happens when I type:
    jar cvmf Sjakk.jar manifest.txt *.class
    java.io.FileNotFoundException: Sjakk.jar (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at sun.tools.jar.Main.run(Main.java:150)
    at sun.tools.jar.Main.main(Main.java:1149)


    Now what?
    Last edited by Igbear; 04-27-2011 at 07:02 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Igbear View Post
    Funny how the order of those four letters make a difference.

    This is what happens when I type:
    jar cvmf Sjakk.jar manifest.txt *.class
    java.io.FileNotFoundException: Sjakk.jar (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at sun.tools.jar.Main.run(Main.java:150)
    at sun.tools.jar.Main.main(Main.java:1149)


    Now what?
    Sorry for the mixup, make that:

    Java Code:
    jar cvmf manifest.txt Sjakk.jar *.class
    Also see this page.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Update:
    I've been speaking with a friend of mine, and following has been done:

    manifest.txt:
    Main-Class: Sjakk.Main
    (open space)

    Command:
    jar cvmf manifest.txt Sjakk.jar Sjakk/*.class
    (In this way pictures and other files are kept away from the jar-file).

    java -jar Sjakk.jar
    This is the result:
    java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:99)
    at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:113)
    at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:240)
    at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:172)
    at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 36)


    NullPointerException. Here is likely the reason the jar-file generated by NetBeans did not work.

    This problems occur even if I include the pictures in the jar-file.
    The image-files are in the same folder as the java-files, and thus the java-codes name the files directly when calling on them for imagedrawing, no subfolder or folder above.
    And even if I exclude the images from the jar-file, I still get the same error if I move the file to the same folder, and run from there.

    What do I do?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    You wrote this:

    Quote Originally Posted by Igbear View Post
    (In this way pictures and other files are kept away from the jar-file).
    Are you sure those images are stored in your .jar file? (check that .jar file manually by using, say, winzip.exe or similar).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Yes, they are stored at the same location as the java-files in the jar-file.
    All picture references are in the same folder

  8. #8
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Searching on the internet, I have possibly found the source of the problem:
    png-files needs to be read in the code through streams, while the coding has focused on direct reading.
    Could anyone provide with an easy example?

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The png "files" are not really files - they are entries in a jar archive. Often you can work with a url:

    Java Code:
    new ImageIcon(StatusBar.class.getResource("/icons/red-16.png");

    where the ImageIcon constructor that takes a URL argument has been used rather than the one that takes a String filename. getResource() returns the correct URL.

    There is also getResourceAsStream() if you want to use an InputStream. Which you might use depends on how you are currently using File instances or String filenames: these are the things you have to replace. Basically look at your current usage of these things and check for other methods/constructors that take URL (or InputStream) arguments.

  10. #10
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Do any of these throw IOException?
    In which case I will have to re-write a lot

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Do any of these throw IOException?

    DId you read the docs ? ;)

    Neither of the Class methods are declared to throw IOException, but they do return null if the resource is not found. This is generally a Good Thing as it represents a programming error (you forgot to put the resource into the jar, or put it in the wrong place, or are looking for it in the wrong place.)

    One gotcha is that some classes are quite happy about null URL arguments. If everything appears to wrok but your images don't show up check that the URL returned was nonnull. null InputStream instances are more straightfoward because your program will just die when you try and use them.

  12. #12
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    As a matter of fact, the method used to get the images is:
    Toolkit.getDefaultToolkit().getImage(Brikke.class. getResource("Name.png"))
    This one is also URL-based, but did not work.

    Seems I have to work with streams

  13. #13
    Igbear is offline Member
    Join Date
    Nov 2010
    Posts
    13
    Rep Power
    0

    Default

    Never mind, the files are locally made.
    Yep, streams, here I come

Similar Threads

  1. pdf file problem
    By idbirohit in forum New To Java
    Replies: 0
    Last Post: 03-03-2011, 12:12 PM
  2. Got problem with jar-file, any help?
    By Igbear in forum Advanced Java
    Replies: 7
    Last Post: 11-20-2010, 09:10 PM
  3. File problem
    By Blackberrylerner in forum New To Java
    Replies: 29
    Last Post: 08-20-2010, 02:00 PM
  4. .BAT File problem
    By egbert95 in forum New To Java
    Replies: 8
    Last Post: 08-03-2010, 01:26 AM
  5. jar file problem
    By nishant.4545 in forum New To Java
    Replies: 1
    Last Post: 07-03-2009, 08:36 PM

Posting Permissions

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