2

I have an application that includes some executable script files. And I know that target machines on which we'll be deploying the application have java. Hence I am packaging the application up in a java archive and extracting on the target machines with "jar xf jarFileName". But the executable scripts are no longer executable. Yes, we can run "chmod _x scriptName". But is it possible for the scripts to be executable after extraction from a jar file without user intervention? I've already read: this related question but that isn't trying to maintain executability.

Community
  • 1
  • 1
markrboyd
  • 49
  • 4
  • 1
    You could use a tar archive instead of a jar, that will preserve the file mode, I don't know a *nix OS that doesn't come with tar. – Mark Harviston Apr 13 '13 at 03:01
  • 1
    @MarkHarviston ...that said, POSIX specifies `pax`, not `tar`, so you're relying on nonstandard (though ubiquitous) extensions in using the latter. (To be fair, however, `pax` supports `ustar`-format archives). – Charles Duffy Apr 13 '13 at 03:52

2 Answers2

0

Well a JAR file is really a ZIP file, so this question covers most of what you are asking: Maintain file permissions when extracting from a zip file using JDK 5 api

The problem is that things like execute bits are OS specific, and there are all sorts of issues in making them work across different operating systems. The "execute" bit is no exception. A typical (external) ZIP file extractor should be able to cope with this (modulo that the relevant info is in the ZIP file in the first place!). But doing this when you extract the ZIP / JAR file programatically would involve unpicking the "extra data" byte array ... I think. I don't know what the jar command does ... though you could look at the source code if you are game ...

If you know that the file / script that you are extracting should be executable, I'd advocate simply modifying your extraction procedure to set the execute bit(s) appropriately after the extraction. In Java 7, you can do this thing the new NIO file attribute APIs. Or run chmod externally. You could also try extracting with a unzip utility rather than jar ... though that assumes that the platform has unzip installed.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I am having the users manually run chmod. Just hoped I was overlooking something that could make that automatic. We'll just keep that documentation in place. – markrboyd Apr 15 '13 at 14:15
0

If you want to deploy on Windows as well as unixes such as OS X, there are DOS/Windows versions of standard utilities such as chmod that are freely available. You could distribute those with your application, most likely, and set the path up so that your Windows chmod.exe is found by your install script if and only if there's not a native version available, as there would be on a unix system. You'd of course have to do enough testing on different systems to make sure that this won't fail in some not-to-unusual context.

Mars
  • 8,689
  • 2
  • 42
  • 70
  • We provide .bat versions in parallel to the .sh versions. And windows just recognizes the extension. So for the windows users there isn't the extra step to make the .bats executable. Thanks. – markrboyd Apr 15 '13 at 14:17