DaMainBoss
DaMainBoss

Reputation: 2003

Using other documents outside a jar file

I just realised that I cant use files from outside a jar archive. If thats the case then when I deploy apps that need other documents, say an xml file, do i send the xml alongside the app or there is a way out..

Thanks

Upvotes: 2

Views: 425

Answers (2)

Mostafa Zeinali
Mostafa Zeinali

Reputation: 2624

But this does not work if you also want to "write" to one of those files!!! If you only want to read, put everything in there. The convention is to create a package called resources in the root of you source code ("src" for example (I use Eclipse)), and just put everything in there, and then use class.getResourceAsStream(). But if you want to write to a File, for example you want persistence for User's options or other stuff, you're gonna need to write from within the .jar, to a File outside the jar, which has a lot of permission considerations and stuff, but It's possible. use System.getProperty("java.class.path"), and you can write files just next to your jar File...

Upvotes: 1

aioobe
aioobe

Reputation: 421220

I'd suggest that you simply include the required resources within the .jar file. You can have any type of files in there, including .xml-files.

Related questions:


If you really prefer to load "external" files you'll have to be more clear about the problems of opening them outside of the jar file.

Upvotes: 1

Related Questions