Solved

Writing to resources withen jar files

Posted on 2006-06-21
7
896 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
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 59
Chrome and Firefox Java 5 67
spring maven example issues 3 38
How to log java errors in tomcat 8 23
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

685 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