如何通过java shell调用cygwin?

问题描述 投票:2回答:1

我有一个用于Linux的代码,它在unix shell上执行.sh文件。

我想为Windows运行相同的代码。我被告知我应该为windows安装cygwin。我这样做但现在如何将命令重定向到cygwin shell?

这是我的代码的一部分:

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脚本?

java cygwin
1个回答
2
投票

执行C:/cygwin/bin/bash.exe(或C:/cygwin/bin/sh.exe,或任何你的Cygwin /bin/<shell>.exe的路径),然后传递脚本作为参数。

还有一个皱纹。没有一个功能齐全的JVM是Cygwin本机,因此您可能正在使用Windows安装和使用的JVM。这意味着'PATH'和其他环境变量的值可能不是您在启动shell时所期望的,并且您可能需要使用cygpath将Java传递的任何路径从Windows路径转换为Cygwin路径,可能包括值0美元。

© www.soinside.com 2019 - 2024. All rights reserved.