Solved

Writing to resources withen jar files

Posted on 2006-06-21
7
908 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 50 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

626 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