java - windows 下unix shell 命令的替代品?

标签 java linux shell

我正在开发一个源代码判断软件。我在Linux平台上从UBuntu 12.04 LTS开发了它。

现在我想将它部署在 Windows 上。 在我的软件中,我根据 unix shell 创建命令,将它们保存在文件中,然后通过文件执行命令。 这是部分代码:

package codejudge.compiler.languages;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import codejudge.compiler.TimedShell;

public class C extends Language {

    String file, contents, dir;
    int timeout;

    public C(String file, int timeout, String contents, String dir) {
        this.file = file;
        this.timeout = timeout;
        this.contents = contents;
        this.dir = dir;
    }
    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");
            out.close();
            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();
        }
    }

    public void execute() {
        try {
            // create the execution script
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir + "/run.sh")));
            out.write("cd \"" + dir +"\"\n");

            out.write("./a.out < in.txt > out.txt");
            out.close();
            Runtime r = Runtime.getRuntime();
            Process p = r.exec(dir + "/run.sh");
            p.waitFor();
            p = r.exec(dir + "/run.sh"); // execute the script
            TimedShell shell = new TimedShell(this, p, 3000);
            shell.start();
            p.waitFor();            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

当我在 Windows 上运行相同的代码时,我收到以下错误(我在 Windows 上安装了 minGW):

Codejudge compilation server running ...
Compiling garima.c...
java.io.IOException: Cannot run program "C:\xampp\htdocs\project\codejudge-compiler\stage\1/compile.sh": CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at codejudge.compiler.languages.C.compile(C.java:41)
    at codejudge.compiler.RequestThread.run(RequestThread.java:65)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more
java.io.FileNotFoundException: C:\xampp\htdocs\project\codejudge-compiler\stage\1\err.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at codejudge.compiler.RequestThread.compileErrors(RequestThread.java:90)
    at codejudge.compiler.RequestThread.run(RequestThread.java:66)
java.io.IOException: Cannot run program "C:\xampp\htdocs\project\codejudge-compiler\stage\1/run.sh": CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at codejudge.compiler.languages.C.execute(C.java:65)
    at codejudge.compiler.RequestThread.run(RequestThread.java:72)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more
java.io.FileNotFoundException: C:\xampp\htdocs\project\codejudge-compiler\stage\1\out.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at codejudge.compiler.RequestThread.execMsg(RequestThread.java:106)
    at codejudge.compiler.RequestThread.run(RequestThread.java:77)

我对 win32 shell 命令知之甚少..需要对代码进行哪些更改,即。哪些命令不能在 Windows 上运行,应该更改哪些命令?它们可以替代 Windows 吗?

最佳答案

您可以使用gnuwin32对于大多数任务。

关于java - windows 下unix shell 命令的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467956/

相关文章:

linux - 在 shell 脚本中列出文件

linux - 运行从 stdin 管道传输的脚本(Linux/Shell 脚本)

java - AKKA .conf 文件配置到 .properties 文件

java - 如何在一个方法中解析不同类型的参数

linux - 如何在 Arch Linux 中同时使用 Super 和 Super+L 作为不同的键盘快捷键?

python - 找不到文件错误 : [Errno 2] No such file or directory: 'ifconfig'

php - 如何在linux中运行后台php进程并结束它?

Java9多模块Maven项目测试依赖

java - 我应该如何构建一个必须使用 Spring 和 JPA 的模块化企业应用程序?

c - 关于标准信号的传递