如何通过Java Shell调用cygwin? [英] How to invoke cygwin through java shell?
问题描述
我有一个用于Linux的代码,该代码在unix shell上执行.sh文件.
I have a code meant for Linux which execute .sh files on the unix shell.
我想为Windows运行相同的代码.有人告诉我应该为Windows安装cygwin.我这样做了,但是现在如何将命令重定向到cygwin shell?
I wanted to run the same code for windows. I was told that I should install cygwin for windows. I did that but now how do I redirect the commands to cygwin shell?
这是我的代码的一部分:
Here is a part of my code:
public void compile() {
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir + "/" + file)));
out.write(contents);
out.close();
// create the compiler script
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir + "\\compile.sh")));
out.write("cd \"" + dir +"\"\n");
out.write("gcc -lm " + file + " 2> err.txt");
Runtime r = Runtime.getRuntime();
Process p = r.exec( dir + "\\compile.sh");
p.waitFor();
p = r.exec(dir + "\\compile.sh"); // execute the compiler script
TimedShell shell = new TimedShell(this, p, timeout);
shell.start();
p.waitFor();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
如何重定向到cygwin Shell并运行Shell脚本?
How do I redirect to cygwin shell and the run the shell scripts?
推荐答案
Exec C:/cygwin/bin/bash.exe
(或 C:/cygwin/bin/sh.exe
或Cygwin /bin/<shell>.exe
的任何路径),然后将脚本作为参数传递.
Exec C:/cygwin/bin/bash.exe
(or C:/cygwin/bin/sh.exe
, or whatever the path to your Cygwin /bin/<shell>.exe
is), and then pass the script as an argument.
不过,还有另外一种皱纹.没有Cygwin原生的功能齐全的JVM,因此您可能会使用Windows安装和使用的JVM.这意味着启动外壳程序时'PATH'和其他环境变量的值可能不是您期望的值,并且您可能需要使用 cygpath
将Java传递的所有路径从Windows路径转换为Cygwin路径,可能包括$ 0的值.
There's one more wrinkle though. There isn't a full-featured JVM that is Cygwin-native, so you'll probably be using the JVM installed and used by Windows. This means that the value of 'PATH' and other environment variables may not be what you expect when you start the shell, and you may need to use cygpath
to convert any paths passed by Java from Windows paths to Cygwin paths, possibly including the value of $0.
这篇关于如何通过Java Shell调用cygwin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!