4

I'm learning java and am currently trying to develop a simple application. My question is can you store data about settings, etc in a text file internal to a .jar? If so how would you go about accessing this within java? Sorry if this is a really stupid idea.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 3
    Yes, but you can't write to it (easily) – MadProgrammer May 23 '13 at 02:21
  • 1
    Look [here](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html) for the `getResourceAsStream(String)` method. You can retrieve the `InputStream` from any resource in the jar. – Sotirios Delimanolis May 23 '13 at 02:22
  • Also, a jar contains (or should contain) a deployment ready application, there is no purpose in inserting anything into it. FYI, a jar is just a fancy java name for a zip file. You can open it with any unzip tool to see what is inside. – Sotirios Delimanolis May 23 '13 at 02:32
  • @MadProgrammer why can't we write data? – Aman Nov 07 '17 at 10:42
  • 1
    @Aman There a re a number reasons - it's a ZIP files, ZIP files can't actually be updated, instead, you copy the contents of existing ZIP file to a temp file, write the new content to it, delete the old ZIP file and rename the temp ZIP file into it's place. To start with, it's not easy to know which JAR file you're accessing (or where it is), second, the OS may lock the JAR (via the classloader) which could prevent it from been deleted – MadProgrammer Nov 07 '17 at 18:43

3 Answers3

3
InputStream is = this.getClass().getResourceAsStream("/data/file.txt");

The resources you are getting need to be on the classpath

mconlin
  • 8,169
  • 5
  • 31
  • 37
2

Yes you can, and it's not a stupid question we all need to start somewhere.

There are two parts to your question:

  1. Adding a data/text file to a .jar - (using ant to jar it:) add "fileset dir=..." to the jar target, where dir is set equal to the directory that has the data/text file. Refer to How can I include data text files in a jar using Ant?

  2. Accessing that data/text file from within the java code - you need to use a ClassLoader and getResourceAsStream. Refer to Loading files in JAR in Tomcat using getResourceAsStream

Also, please take a look at https://github.com/gitjonathan/turbo-sansa, I have a working version up on it.

Community
  • 1
  • 1
jonm
  • 21
  • 4
0

Can you store data inside a .jar?

Read-only data can be stored inside a JAR file. You can read such data using getResourceAsStream(...) if the JAR is on the classpath, or by using the standard JAR file API class if it is not on tle classpath.

But storing update-able data in a JAR file is problematic:

  • In a lot of circumstances it is impossible; e.g. because the JAR file is read-only or was downloaded on the fly.

  • In all other cases it would be very awkward, because the standard JAR file API class does not support update in place. (You would need to create a new ZIP file, copy across the old content apart from the file you are updating, add that file, and then rename the resulting file.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216