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?

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?

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.)

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

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.

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);

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!

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.