Solved

Writing to resources withen jar files

Posted on 2006-06-21
7
883 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
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now