Results 1 to 9 of 9
  1. #1
    erhart is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default Text and image files within jar files

    Hi all,
    I am new to using jar files, and I am having a problem accessing files within the jar file. I packaged the jar file using Eclipse. The error that I get is:

    java.io.FileNotFoundException: Coordinates.txt <The system cannot find the file specified>
    at java.io.FileInputStream.open<Native Method>
    at java.io.FileInputStream.<init><Unknown Source>
    at java.util.Scanner.<init><Unknown Source>

    I can fix this problem by copying and pasting the contents of my files (.txt and .gif) into completely new files and replacing the old ones using WinRAR. When I do this, the program runs perfectly. Unfortunately, when I email my program as an attachment or on some computers when I transfer it with a thumb drive, I get the same error message. Because of the patched-up fix that I found, I am wondering if this could be a problem with file permissions?

    Thanks a lot!

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    10

    Default

    Please include the code you used to access those files in jar archive. You use Class.getResourceAsStream..
    dont worry newbie, we got you covered.

  3. #3
    erhart is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    For text files, I use:

    Java Code:
    private Scanner openFileForReading(String fileName)
    	{
    		try{
    			scanner = new Scanner (new File(fileName));		
    		}
    		catch (FileNotFoundException e){
    			System.out.println( "Could not open " +
    					fileName);
    			e.printStackTrace();
    		}
    		return scanner;
    	}
    For image files, I use:

    Java Code:
            private Image getCelebrityImage(String fileName){
        		BufferedImage img = null;
        		try {
        			img = ImageIO.read(new File(fileName));
        		} 
        		catch (IOException e) {
        			System.out.println("Unable to access image " + fileName);
        			e.printStackTrace();
        		}
        		return img;
            }

  4. #4
    afsina is offline Member
    Join Date
    Jan 2008
    Posts
    24
    Rep Power
    0

    Default

    The files in jar files are treated as "Resources". you need to access them as a classpath resource, regular File access methods does not work there. However, it is very easy. check the javadoc of "Class.getREsourceAsStream()". So, what you need is:
    - the location-name of the resource in the jar (you will need to add a "/" symbol when writing the name.)
    - How will you get the Class? try finding this out first
    - This is suppose to return a strem. you already have the code for Scanner or BufferedImage. check their constructors if any accepts an inputstream..
    write again if there is a problem.
    Last edited by afsina; 01-12-2008 at 11:28 PM.

  5. #5
    erhart is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Thanks a lot for the help! This solves almost all the problems. One question, though: I also want to write to some text files. A PrintWriter constructer takes an OutputStream, but I'm not sure how to get one as a Resource. Does anyone know how to do that?

  6. #6
    erhart is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Never mind, I found a way:

    PrintWriter p = new PrintWriter(new File(getClass().getResource(fileName).toURI()));

    I really appreciate the help!

  7. #7
    jagadeesh0014 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    This tutorial is more help to me tnk u

  8. #8
    jagadeesh0014 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Thumbs up Ejb

    Hi
    my name is jagadeesh
    q: How to run jsp in tomcat
    I need step by step in Image viewing
    Tnk u

  9. #9
    jagadeesh0014 is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi All iam new in server side programs so i need u r help to run jsp and ejb , structs
    Tnk u

Similar Threads

  1. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 02:29 PM
  2. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:15 AM
  3. Writing to files within jar files
    By erhart in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 03:50 AM
  4. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 AM
  5. convert xls files into pdf files
    By bbq in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 03:56 AM

Posting Permissions

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