Load images and text file from jar file

Discussion in 'Java' started by Fuma, Jun 20, 2004.

  1. Fuma

    Fuma Guest

    Hello,
    I know this question has been asked before, but I cannot seem to
    get the solutions posted to work. I am trying load to three images and
    a text file from a jar file (this jar file is not in the classpath).
    This jar file is the main jar file though. I intend for users to be
    able to download this file to their desktop, double click, and run.
    Unfortunately, I cannot figure out how to load the three images and
    textfile from the jar file.

    I have tried this solution but it doesnt want to work:

    URL url = this.getClass().getResource("Teacher_online.jpg");
    image = new ImageIcon( url );

    and it says it can not file the file. And for the text file i have

    URL url = this.getClass().getResource("config_student.ini");
    ins = new InputStreamReader(url.openStream());
    bufferedReader = new BufferedReader( ins );

    If somone could shed some light on this problem i am having, i would
    really appreciate it!

    Thanks,
    Chris
    Fuma, Jun 20, 2004
    #1
    1. Advertising

  2. Fuma

    Filip Larsen Guest

    Fuma wrote

    > I have tried this solution but it doesnt want to work:
    >
    > URL url = this.getClass().getResource("Teacher_online.jpg");
    > image = new ImageIcon( url );
    >
    > and it says it can not file the file.


    As the documentation says, the Class.getResource method will prepend the
    "package path" to resource names that do not begin with a slash. In your
    case, if file is placed as images/myfile.jpg in your jar-file you
    probably want to call it with .getResource("/images/myfile.jpg").


    Regards,
    --
    Filip Larsen
    Filip Larsen, Jun 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucy
    Replies:
    6
    Views:
    30,634
    steve
    Sep 7, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    568
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,719
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,754
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    657
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page