用 Java 为 OSX 编写一个可执行的 .sh 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/673685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write an executable .sh file with Java for OSX
提问by Glenn
So I am trying to write an .sh file that will be executable, this is how I'm currently writing it:
所以我正在尝试编写一个可执行的 .sh 文件,这就是我目前的编写方式:
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.
编辑:为了进一步澄清,我试图让它默认执行,例如,如果你双击生成的文件,它会自动执行。
回答by John Feminella
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 chmodit yourself with a system call with Process.
您可以调用File.setExecutable()为文件设置所有者的可执行位,这对于您的情况可能就足够了。或者您可以chmod自己使用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.
唉,在 Java 7 之前,无法对文件权限进行全面的编程更改。它将成为新 IO 功能集的一部分,您可以在此处阅读更多相关信息。
回答by user49913
You'd need to chmod it, and you can probably do it by exec'ing a system command like such:
您需要对其进行 chmod,并且您可以通过执行如下系统命令来实现:
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:
但是如果你想更明确地跟踪它可以捕获 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()));
Which I got from here: http://www.devdaily.com/java/edu/pj/pj010016/pj010016.shtml
我从这里得到的: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.
在 linux 上,如果您打开文件浏览器并双击 /tmp/toast.sh 并选择运行它,它应该会生成一个文本文件 /tmp/toast.txt,其中包含“yeah toast”字样。我认为 Mac 会做同样的事情,因为它的底层是 BSD。
回答by mkobit
In Java 7 you can call Files.setPosixFilePermissions. Here is an example:
在 Java 7 中,您可以调用Files.setPosixFilePermissions. 下面是一个例子:
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);
}
}
回答by mouviciel
On Mac OS X, besides chmod +x, you have to give a .commandextension to your shell script if you want to launch it with a double-click.
在 Mac OS X 上,除了 chmod +x,.command如果你想双击启动它,你必须给你的 shell 脚本一个扩展。
回答by Marty Lamb
This answerI wrote for the question how do I programmatically change file permissionsshows a chmod example via a native call using jna, which should work on Mac OS X.
我为如何以编程方式更改文件权限的问题编写的这个答案通过使用 jna 的本机调用显示了一个 chmod 示例,它应该适用于 Mac OS X。

