954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File IO outside of JAR

Hi everyone

I have a project that I have created in Netbeans.

I would like to deploy it as a JAR file, so i have "build"'ed it and I have a jar which runs nicely in the /dist folder.

Problem is, the program in question requires reading from and writing to, the same file.

Now I have read somewhere that it is not possible to write to files within a jar file, so I want to access a resource outside of the jar file.

The thing is, I have searched and searched and I still cannot find a way to access and write to files outside of the JAR file?

Any pointers?

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

The normal way for a java program to access and write files has nothing to do with whether the code is in a jar. If you can write a program to read and write to a file using files vs resources, that class will work the same if it is in a jar or if the program classes are in a folder.
I want to access a resource outside of the jar file
Can you change the code to use files vs resources?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I certainly can, that was how I had it before I started using URLs and .getResource()

However, the reason I used .getResource was so that I didn't have to specify absolute paths; I wanted the folder to be mobile (I can copy the folder to another PC and it will work fine.)

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

Can you use relative paths?
Who decides where to put the files you want to be able to read/write?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I'm using NetBeans, as far as I know it doesn't allow for relative paths. I decide where I want them to be. For example, all I want to do is put the text file in the /dist folder with the JAR and then put that folder anywhere and it will run and be able to find the file.

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

Strange behavior for your IDE. You mean you can't code:
String path = "aFolder/Afile.txt"; // relative path to the file
File aFile = new File(path);

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

EDIT:

It appears that that does work. I feel silly, and annoyed because I have never been able to get this to work and now it does.

Thank you!

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

Where are you putting the jar? It should treat the jar file location as the root and paths relative to that location should resolve just fine.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

IDEs can be confusing. Lots of power, but lots to learn.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: