salocinx
salocinx

Reputation: 3823

How to preserve file permissions with Java across different operating systems?

I'm currently developing a cross-platform application (Windows, Mac OS X, Linux) with Java and building the project on Windows. During the build process, I construct a shell-script for Linux to "wrap" the executable jar-file.

I set the executable bit of the shell script as follows:

File script = new File("run.sh");
script.setExecutable(true);

But when transferring the run.sh file to Linux, I always end up with the following permissions:

-rw-r--r-- 1 salocinx salocinx 120 2014-08-20 20:21 run.sh

Anybody knows how to preserve the x-bit from Windows to Linux ?

Upvotes: 0

Views: 1285

Answers (2)

Karol S
Karol S

Reputation: 9402

setExecutable doesn't do anything on Windows, it returns false if it fails, you can check.

Windows filesystems have no concept of executable permission.

What permissions files have after transferring to Linux, depends on the program you used to transfer them, but usually they'll be 600 or 644. To solve this, you have to set the executable bit under a Unix-like operating system on a suitable filesystem.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201399

Your file transfer program would need a preserve permissions option. I don't think you're going to find one. You could run your program on the unix box to change the file permissions, or you could run a find command like

find . -name "*.sh" -exec chmod a+x {} \;

Or you could try setting your umask,

umask u+x

Or (if you're using cygwin), you could use tar with

tar cfp file.tar <scripts>

note that p is preserve permissions.

Upvotes: 2

Related Questions