8

So I am trying to write an .sh file that will be executable, this is how I'm currently writing it:

Writer output = null;

try {
  output = new BufferedWriter(new FileWriter(file2));
  output.write(shellScriptContent);
  output.close();
} catch (IOException ex) {
  Logger.getLogger(PunchGUI.class.getName()).log(Level.SEVERE, null, ex);
}

So that writes the file just fine, but it is not executable. Is there a way to change the executable status when I write it?

Edit: To further clarify, I am trying to make it execute by default, so that for instance, if you double clicked the generated file, it would automatically execute.

Glenn
  • 4,195
  • 9
  • 33
  • 41

5 Answers5

27

You can call File.setExecutable() to set the owner's executable bit for the file, which might be sufficient for your case. Or you can just chmod it yourself with a system call with Process.

Alas, full-powered programmatic alteration of file permissions isn't available until Java 7. It'll be part of the New IO feature set, which you can read more about here.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • Thanks, but unfortunately, I'm working with 1.5. It has to have pretty broad compatibility for OSX, and most people don't have 1.6. – Glenn Mar 24 '09 at 02:24
  • In that case, you're stuck with my alternate solution of using chmod with a system call. Sorry! – John Feminella Mar 24 '09 at 02:28
9

You'd need to chmod it, and you can probably do it by exec'ing a system command like such:

Really all you'd need is to fire off something like this:

Runtime.getRuntime().exec("chmod u+x "+FILENAME);

But if you want to keep track of it more explicitly can capture stdin / stderr then something more like:

Process p = Runtime.getRuntime().exec("chmod u+x "+FILENAME);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));    
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

Which I got from here: http://www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml

Update:

Test program:

package junk;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Main{
  private String scriptContent = '#!/bin/bash \n echo "yeah toast!" > /tmp/toast.txt';
  public void doIt(){
    try{
      Writer output = new BufferedWriter(new FileWriter("/tmp/toast.sh"));
      output.write(scriptContent);
      output.close();
      Runtime.getRuntime().exec("chmod u+x /tmp/toast.sh");
    }catch (IOException ex){}
  }

  public static void main(String[] args){
    Main m = new Main();
    m.doIt();
  }

}

On linux if you open up a file browser and double click on /tmp/toast.sh and choose to run it, it should generate a text file /tmp/toast.txt with the words 'yeah toast'. I assume Mac would do the same since it's BSD under the hood.

user49913
  • 3,018
  • 1
  • 17
  • 8
  • Thanks for the suggestion! Will this also make it automatically execute when double clicked? – Glenn Mar 23 '09 at 15:01
  • It should, assuming the user running the java program is the same who will run the shell script. Otherwise you could adjust the chmod to a+x which means all users have execute permission. I did a small test program on linux and it worked for me. – user49913 Mar 23 '09 at 15:44
  • You saved the day dude, i was getting around how to execute a script from within java and turns out all i needed to do was make it executable via the `chmod` command ! Thanks – redDragonzz Jan 17 '13 at 02:11
6

In Java 7 you can call Files.setPosixFilePermissions. Here is an example:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

class FilePermissionExample {
  public static void main(String[] args) throws IOException {
    final Path filepath = Paths.get("path", "to", "file.txt");
    final Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(filepath);
    permissions.add(PosixFilePermission.OWNER_EXECUTE);
    Files.setPosixFilePermissions(filepath, permissions);
  }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
2

On Mac OS X, besides chmod +x, you have to give a .command extension to your shell script if you want to launch it with a double-click.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
1

This answer I wrote for the question how do I programmatically change file permissions shows a chmod example via a native call using jna, which should work on Mac OS X.

Community
  • 1
  • 1
Marty Lamb
  • 1,980
  • 1
  • 14
  • 10