从另一个java文件执行java命令

时间:2014-02-14 14:55:58

标签: java swing command-line

public String runMsg(String fileName, File Path, int option) 
{
    int x = 0;
    String name = " ", ret = "";

    if (option == 1) {
        x = fileName.indexOf(".c");
        name = fileName.substring(0, x);
        command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
    } else if (option == 2) {
        x = fileName.indexOf(".cpp");
        name = fileName.substring(0, x);
        command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
    } else {
        x = fileName.indexOf(".java");
        name = fileName.substring(0, x);
        command = "java " + name + " < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
    }

    String output = executeCommand(command, Path);
}

private String executeCommand(String command, File Path)
{
    StringBuffer output = new StringBuffer();
    Process p;

    try {
        p = Runtime.getRuntime().exec(command, null, Path);
        p.waitFor();

        BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
        // BufferedReader reader3 = new BufferedReader(new InputStreamReader(p.getOutputStream()));

        String line = "";
        while ((line = reader1.readLine()) != null) {
            output.append(line + "\n");
        }
        while ((line = reader2.readLine()) != null) {
            output.append(line + "\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();
}

我正在尝试从另一个java文件执行.class文件。 .java文件将从位于C:\iptest\input.txt的txt文件中获取输入,并将在以下位置生成输出文件C:\outputtest\out.txt但是当我运行此应用程序时没有任何反应,应用程序进入某些一种无限循环。

0 个答案:

没有答案