?
Solved

Writing to resources withen jar files

Posted on 2006-06-21
7
Medium Priority
?
910 Views
Last Modified: 2012-06-22
Hi everyone,

I've an application that reads/writes to some text files.  When it came to packaging the application into a jar file I could not access the resources (text files) of my application using this code:

URL url = Class.forName("someclass").getResource("/resources/text/somefile.txt");
String resourcePath = url.toURI().getPath();
InputStream stream = new FileInputStream(new File(resourcePath));

Fortunately, I could easily replace it with this code:

InputStream stream = Class.forName("someclass").getResourceAsStream("/resources/text/somefile.txt");

However, I failed to find a way to get the same file into an OutputStream to be able to write to it.

Any suggestion please?
0
Comment
Question by:The14follower
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 200 total points
ID: 16954035
you can not write to resources inside jar
0
 

Author Comment

by:The14follower
ID: 16954082
Thanks hoomanv.

Any article can confirm this before I accept your answer?
0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 16954089
The resource does not exist as an independent file, which is why using a FileInputStream does not work.

If you want to rewrite the resource that exists within the jar file, you'll have to rewrite the entire jar file (which may have other complications).

If you simply want to write the resource to a new file, you need to continually read from the InputStream, writing to a FileOutputStream until the input stream reaches EOF.

If you want the "new" resource to override the old one, you can always first check for the file-based resource, then fall back to the one in the jar file.  This would be far simpler than rewriting the jar file.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:hoomanv
ID: 16954185
> URL url = Class.forName("someclass").getResource("/resources/text/somefile.txt");

JarURLConnection instances can only be used to read from JAR files. It is not possible to get a OutputStream to modify or write to the underlying JAR file using this class.
from: http://java.sun.com/j2se/1.5.0/docs/api/java/net/JarURLConnection.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16954263
You can't write to a jar. You need to write to a file then rejar it
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16954376
The14follower, please believe all above.

;JOOP!
0
 

Author Comment

by:The14follower
ID: 16955635
Thanks again,

It wasn't a matter of believing the answer.  Delaying that acceptance was just to gather other experts' ideas.

It seems that my application needs to be revised to dump its dynamic resources outside the jar file.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question