Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2008, 10:37 AM
Member
 
Join Date: Jan 2008
Posts: 7
erhart is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-12-2008, 10:39 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 260
roots is on a distinguished road
Please include the code you used to access those files in jar archive. You use Class.getResourceAsStream..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 09:51 PM
Member
 
Join Date: Jan 2008
Posts: 7
erhart is on a distinguished road
For text files, I use:

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:

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; }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-12-2008, 11:17 PM
Member
 
Join Date: Jan 2008
Posts: 24
afsina is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-15-2008, 06:51 AM
Member
 
Join Date: Jan 2008
Posts: 7
erhart is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-15-2008, 07:06 AM
Member
 
Join Date: Jan 2008
Posts: 7
erhart is on a distinguished road
Never mind, I found a way:

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

I really appreciate the help!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-19-2008, 05:36 AM
Member
 
Join Date: Jan 2008
Posts: 3
jagadeesh0014 is on a distinguished road
This tutorial is more help to me tnk u
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-19-2008, 05:40 AM
Member
 
Join Date: Jan 2008
Posts: 3
jagadeesh0014 is on a distinguished road
Ejb
Hi
my name is jagadeesh
q: How to run jsp in tomcat
I need step by step in Image viewing
Tnk u
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-19-2008, 05:43 AM
Member
 
Join Date: Jan 2008
Posts: 3
jagadeesh0014 is on a distinguished road
Hi All iam new in server side programs so i need u r help to run jsp and ejb , structs
Tnk u
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 05:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org