Solved

Writing to resources withen jar files

Posted on 2006-06-21
7
886 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

12 Experts available now in Live!

Get 1:1 Help Now