你需要 chmod 它,你可以通过执行这样的系统命令来做到这一点:
你真正需要的只是启动这样的东西:
Runtime.getRuntime().exec("chmod u+x "+FILENAME);
但是,如果您想更明确地跟踪它,可以捕获 stdin / stderr 然后更像:
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()));
我从这里得到的:http:
//www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml
更新:
测试程序:
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();
}
}
在 linux 上,如果您打开文件浏览器并双击 /tmp/toast.sh 并选择运行它,它应该会生成一个文本文件 /tmp/toast.txt,其中包含“yeah toast”。我认为 Mac 会做同样的事情,因为它的底层是 BSD。