Results 1 to 9 of 9
- 01-12-2008, 09:37 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
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!
- 01-12-2008, 09:39 AM #2
Please include the code you used to access those files in jar archive. You use Class.getResourceAsStream..
dont worry newbie, we got you covered.
- 01-12-2008, 08:51 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
For text files, I use:
For image 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; }
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; }
- 01-12-2008, 10:17 PM #4
Member
- Join Date
- Jan 2008
- Posts
- 24
- Rep Power
- 0
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 10:28 PM.
- 01-15-2008, 05:51 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
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?
- 01-15-2008, 06:06 AM #6
Member
- Join Date
- Jan 2008
- Posts
- 7
- Rep Power
- 0
Never mind, I found a way:
PrintWriter p = new PrintWriter(new File(getClass().getResource(fileName).toURI()));
I really appreciate the help!
- 01-19-2008, 04:36 AM #7
Member
- Join Date
- Jan 2008
- Posts
- 3
- Rep Power
- 0
This tutorial is more help to me tnk u
- 01-19-2008, 04:40 AM #8
Member
- Join Date
- Jan 2008
- Posts
- 3
- Rep Power
- 0
Ejb
Hi
my name is jagadeesh
q: How to run jsp in tomcat
I need step by step in Image viewing
Tnk u
- 01-19-2008, 04:43 AM #9
Member
- Join Date
- Jan 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Does OS intervene when reading Java text files
By Tina G in forum Advanced JavaReplies: 1Last Post: 04-07-2008, 02:29 PM -
Applet - reading text files packed into JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:15 AM -
Writing to files within jar files
By erhart in forum Advanced JavaReplies: 0Last Post: 02-04-2008, 02:50 AM -
how to convert mpeg files to .wav files
By christina in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:14 AM -
convert xls files into pdf files
By bbq in forum New To JavaReplies: 3Last Post: 07-20-2007, 03:56 AM


LinkBack URL
About LinkBacks

Bookmarks