我正在编写一个带有Java GUI的应用程序,它调用了一些FORTRAN代码。我想返回一个文件(solution.ps),它根据FORTRAN代码中的更改进行更新和编译,这些更改是在我的ActionPerformed方法中创建的。但是,我目前的代码只返回旧版本的文件,而不是等待cmd编译的更新结果。有没有办法让cmd在完成下一步之前等待进程运行? (它可以直接从cmd运行)
我已搜索但除了process.waitFor()之外找不到任何内容,这似乎不会在正确的位置暂停执行。尝试了Thread.waitFor()。
我认为这对于想要将用户输入发送到另一个程序并返回使用这些输入的编译结果的人来说非常有用。
无论如何这里是代码,提前感谢任何帮助,我希望我能清楚地解决问题。
String[] command ={"cmd",};
try {
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("cd c:\\g77");
stdin.println("g77setup.bat");
stdin.println("cd c:\\users\\laurence\\workspace\\areaplanner");
stdin.println("g77 -O4 genpack.f -o genpack");
stdin.println("genpack");
stdin.println("5");
/*
* The following line sets the time to run the FORTRAN code for
* - need to wait for this to complete before calling mpost
*/
stdin.println("30");
stdin.println("mpost solution.mp");
stdin.println("latex solution.tex");
stdin.println("dvips solution.dvi -o solution.ps");
stdin.close();
} catch(IOException e4){}
答案 0 :(得分:3)
您只能运行Windows shell命令。要修复,建议先编写批处理文件并等待它完成:
String command = "cmd /c mybatchfile.bat";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
要在第一组命令完成之前让另一部分启动,您必须编写另一个批处理文件并重复上述步骤。确保你有两个进程然后在不同的线程中。
答案 1 :(得分:-1)
尝试使用waitFor以使当前线程等待进程完成其工作。
Process p = Runtime.getRuntime().exec(command);
p.waitFor()
代码中的命令不完整。并且建议使用ProcessBuilder.start()
代替Process
。