Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my Java program store files inside of its .jar file?

Tags:

java

jar

archive

I know that .jar files are basically archives as well as being applications. What I'm asking is how can I store data(actual files not just strings) packed inside my program? I want to do this within my Java code.

The reason for this if your wondering is that I'm producing a server mod of a game. The server starts and creates all the level data and I want to store all these file inside my .jar app.

like image 726
liamzebedee Avatar asked Oct 30 '10 00:10

liamzebedee


2 Answers

Yes you can do this.

Non-code resources in a JAR file on the classpath can be access using Class.getResourceAsStream(String). Applications routinely do this, for example, to embed internationalized messages as resource bundles.

To get your file into the JAR file, just copy it into the appropriate place in the input directory tree before you run the jar command.

FOLLOW UP

In theory, your application could store files inside its own JAR file, under certain circumstances:

  • The JAR has to be a file in the local file system; i.e. not a JAR that was fetched from a remote server.
  • The application has to have write access to the JAR file and its parent directory.
  • The application must not need to read back the file it wrote to the JAR in the current classloader; i.e. without exiting and restarting.
  • The JAR must not need to be be signed.

The procedure would be:

  1. Locate the JAR file and open as a ZIP archive reader.
  2. Create a ZIP archive writer to write a new version of JAR file.
  3. Write the application's files to the writer.
  4. Write all resources from the ZIP reader to the writer, excluding old versions of the applications files.
  5. Close the reader and writer.
  6. Rename the new version of the JAR to replace the old one.

The last step might not work if the initial JAR is locked by the JVM / OS. In that case, you need do the renaming in a wrapper script.

However, I think that most people would agree that this is a BAD IDEA. It is simpler and more robust to just write regular files.

like image 68
Stephen C Avatar answered Sep 30 '22 05:09

Stephen C


The other answers have provided some good strategies, but I am going to suggest going in a somewhat different direction.

This game supposedly has graphics and is a desktop application. It is most easy to distribute desktop applications from a web server.

If both those things are true of your game, then look into using Java Web Start to deploy it.

JWS offers APIs not available to other apps. & one of particular interest to this problem is the PersistenceService. The PersistenceService allows for small amounts of data to be stored and restored by an app. (even when it is in a sand-box). I have made a small demo. of the PersistenceService.

The idea would be to check the PersistenceService for the application data, and if not found, use the data in the Jars. If the user/application alters the data, write the altered data to the PersistenceService.


JWS also offers other nice features like splash screens, desktop integration, automatic updates..

like image 45
Andrew Thompson Avatar answered Sep 30 '22 04:09

Andrew Thompson