如何调用cygwin thorugh java shell? [英] how to invoke cygwin thorugh java shell?

查看:299
本文介绍了如何调用cygwin thorugh java shell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



这是我的代码的一部分:

  public void compile(){
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir +/+文件)));
out.write(contents);
out.close();
//创建编译器脚本
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir +\\compile.sh)));
out.write(cd \+ dir +\\\\
);
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); //执行编译器脚本
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脚本?
代码示例将非常有用的感谢很多。 c> C:/ cygwin / bin / bash。 .exe (或 C:/cygwin/bin/sh.exe 或任何路径到您的Cygwin / bin /<shell>.exe is),然后将脚本作为参数传递。



还有一个皱纹。没有一个全功能的JVM是Cygwin本地的,所以你可能会使用JVM安装和使用的Windows。这意味着'PATH'和其他环境变量的值可能不是您在启动shell时所期望的值,您可能需要使用 cygpath 来转换任何传递的路径通过Java从Windows路径到Cygwin路径,可能包括$ 0的值。


I have a code meant for Linux which execute .sh files on the unix 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();
    }
}

How do i redirect to cygwin shell and the run the shell scripts? A code example would be really helpful thanks a lot. been bugging myhead for a couple of days on this

解决方案

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.

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.

这篇关于如何调用cygwin thorugh java shell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆