Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: my program works but when i export it as runnable jar file it doesn't works..

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my program works but when i export it as runnable jar file it doesn't works..

    it is a simple gui program with pictures.
    -when i run it from cmd it says it cant load the pictures.
    -when i run it from elipse it works.

    the code looks like this-
    try {
    faces[0].Questionset(ImageIO.read(new File("src\\Cristiano.jpg")),"cristiano ronaldo");
    } catch (IOException e) {

    e.printStackTrace();
    }
    try {
    faces[1].Questionset(ImageIO.read(new File("src\\Jim.jpg")),"jim carry");
    } catch (IOException e) {

    e.printStackTrace();
    }
    etc

    how can i fix that?


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    Hello.
    When you run at the command prompt you will need to do the appropriate CLASSPATH settings. Check if you have done that?

    Syed.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    I had this trouble a few months back. There was a little disagreement on the source of the trouble. It looks like you're already using ImageIO.read, which ended up being the solution for me. It might be the way you're reading the files. I think those slashes in the file path are supposed to go the other way.

    Here is the link to my thread with a similar problem. Hopefully that will help.

    Also, used the forum software's code tags ( that's {code="java"}{/code} but with square brackets instead of curly ones) to post code segments. It makes it much easier to read.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    Can you put the image files in the jar file and use the getResource() method to get a URL to read them instead of trying to read the files from the PC's disk?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    it seems to work only when i put the dictionary src in the same folder as the jar file..
    how can i use getResource()?
    i tried
    faces[0].Questionset(ImageIO.read(getResource("src\\Cristi ano.jpg")),"cristiano ronaldo");
    but it returns null..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    Is the src folder in the jar file or on the claspath? The getResource() method looks for the resource on the classpath. When using java -jar to execute the contents of a jar file, the jar file's contents is on the classpath.

    --- Update ---

    Use / not \ in the resource path.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    in the classpath path.
    so what should i write instade of
    faces[0].Questionset(ImageIO.read(getResource("src//Cristiano.jpg")),"cristiano ronaldo");
    ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    Use a single / in the path. You need two \\s because the \ is the "escape" character for the compiler.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    Quote Originally Posted by Norm View Post
    Use a single / in the path. You need two \\s because the \ is the "escape" character for the compiler.
    not working
    faces[0].Questionset(ImageIO.read(getResource("src/Cristiano.jpg")),"cristiano ronaldo");

    what to do?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my program works but when i export it as runnable jar file it doesn't works..

    What is the full text of the error message?

    Try doing some debugging to find where the problem is:
    What does the getResource() method return?
    What does the ImageIO.read() method return?

    Break the compound statement you are using into single, simple statements and print the results returned by each method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I cant export my applet into a runnable jar file
    By Shef012 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2013, 02:08 PM
  2. Math.PI sometimes works, and sometimes doesn't. Help!?
    By RedCloudTsunami in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 30th, 2012, 04:00 PM
  3. [SOLVED] Jar file only Works on my computer?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 42
    Last Post: January 8th, 2012, 07:20 AM
  4. Program works fine but with friend it doesn't work
    By Rizza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2011, 01:20 PM
  5. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 09:19 AM