3

I would like to know how to create a jar file that preserves the file permissions of its content.

I'm packaging up source and executables in a jar file that will be extracted before use. People should be able to immediately run the examples and demonstrations by running batch / shell script files. Then they should be able to modify source and re-compile everything.

I'm trying to make life easy for people who use it and this includes people who are new to Java and programming who might be working on Linux / Unix.

I have access to machines running Windows and Linux. I don't use Linux a lot so maybe I've done something wrong.

I created a jar file on Linux using jar cf .... after setting permissions on the files so that everything worked as needed. Then I created a new test directory and extracted the jar file contents into it. The file permissions were not preserved ... i.e. -rw-rw-r--

Roger F. Gay
  • 1,830
  • 2
  • 20
  • 25
  • Is there a reason you have to use jar? It's based on the zip format which AFAIK has no way to store permission/owner metadata. Tar (tar.gz, tar.bz2) is good for that. Alternately, do you really need executable shell scripts? Assuming this is Java, the obvious way to run things is `java `. – jwelsh Jan 27 '15 at 19:31
  • I chose jar over zip because zip was requiring download acceptance - probably considering the risk of executable files it contained. But you're saying that neither of those is a good alternative anyway. I'm open to other possibilities. My goal is to provide it in a way that is easy for Java programmers, without requiring more than a Java programmer needs (like the tools that come with the JDK), on both Windows and Linux. It's partly philosophical. I put a lot of effort into building things that are flexible and powerful in use, and easy to use. This is my first product offering, so this final – Roger F. Gay Jan 27 '15 at 20:37

1 Answers1

4

The jar tool doesn't store file permissions, so you can't recover what has not been stored. jar format is not intended to archive files and metadata as zip or tar, it is a simple container to embed java files for an application.

dbort
  • 962
  • 8
  • 15
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • I keep reading about setting up a meta file for execution (executable jar files) and continued to wonder whether that might be an approach to assuring that files within remain executable. – Roger F. Gay Jan 27 '15 at 20:39
  • 1
    Accepted. Being disappointed is beside the point. Facts are facts. The download is for Java program developers. They'll have the jar tool regardless of os. Linux users will have to understand they they need to deal with permissions. – Roger F. Gay Jan 28 '15 at 22:14
  • Actually extensions to the zip/jar format allow restoring file permissions in linux, see https://stackoverflow.com/questions/13633488/can-i-store-unix-permissions-in-a-zip-file-built-with-apache-ant – tkruse Feb 22 '19 at 03:26