2

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

DaMainBoss
  • 2,035
  • 1
  • 19
  • 26

2 Answers2

1

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

Mostafa Zeinali
  • 2,456
  • 2
  • 15
  • 23
1

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.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I tried but ddnt work. I was told that you cant put things like txt files or whatever in the jar and access them during runtime – DaMainBoss May 19 '11 at 18:22
  • The person who told you that was lying. You can put what ever files you like in there. The `.jar` file is in fact actually a `.zip` file. See the first related-question link for instance. – aioobe May 19 '11 at 18:23