2012-07-25 62 views
3

我正在写一个Java GUI的应用程序,它调用一些FORTRAN代码。我想返回一个文件(solution.ps),该文件基于FORTRAN代码中的更改进行更新和编译,这些更改是在我的ActionPerformed方法中创建的。然而,我目前使用的代码只是返回文件的旧版本,而不是等待cmd编译的更新结果。有没有办法让cmd在完成下一步之前等待进程运行? (它工作正常运行直接从cmd)cmd java等待进程

我已经搜索,但无法找到任何东西,除了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

我不明白你的问题。这是一个CMD或Java问题?等待流程在完成下一步之前运行?_下一步是什么? – poussma 2012-07-25 12:03:34

+0

两者,我认为。我不希望线路“mpost solution.mp”被调用,直到FORTRAN代码运行30秒。在上面的FORTRAN代码运行30秒,但在上面的代码中重新编译之前,上面代码片段后面的Java代码将返回旧版本的solution.ps。 – user1551429 2012-07-25 12:07:14

回答

3

您只能运行windows shell命令。要修复,建议先写批处理文件,并等待其完成:

String command = "cmd /c mybatchfile.bat"; 
Process p = Runtime.getRuntime().exec(command); 
p.waitFor(); 

再弄段的第一组命令已完成之前揭开序幕,你将不得不写另一个批处理文件并重复上述。确保你有两个进程然后在不同的线程。

+1

我一直在尝试这个,我几乎得到了一个完整的工作解决方案。这绝对是走下坡路,感谢您的帮助! – user1551429 2012-07-26 10:32:03

+0

没问题。随时'剔'我的回应:) – Reimeus 2012-07-26 10:35:36

-1

尝试使用waitFor以使当前线程等待进程完成其作业。

Process p = Runtime.getRuntime().exec(command); 
p.waitFor() 

代码中的命令不完整。并且建议使用ProcessBuilder.start()而不是Process