Search code examples
linuxwindowsfilefile-permissionspreserve

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 ?


Solution

  • 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.