2016-07-23 83 views
0

我想編譯一個java文件並在另一個類中執行它的類(這個類是一個Spring MVC項目的@service)。ProcessBuilder&Runtime exec在Spring項目中找不到或加載主類

的服務代碼是:

@Service 
public class MRServiceImp implements MRService { 
    @Override 
    public String submitMR(int id, String fd) { 
     try { 
      // compile the java file 
      String[] cmd = {"javac", "P" + id + ".java"}; 
      ProcessBuilder pb = new ProcessBuilder(cmd); 
      pb.directory(new File(fd)); 
      Process p = pb.start(); 

      // exec the class file 
      String[] execmd = {"java", "P" + pz_id}; 
      ProcessBuilder epb = new ProcessBuilder(execmd); 
      epb.directory(new File(fd)); 
      p = epb.start(); 

      // get normal output 
      BufferedReader pin = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String ptmp = pin.readLine(); 
      while (ptmp != null) { 
       pout = pout == null ? ptmp + '\n' : pout + ptmp + '\n'; 
       ptmp = pin.readLine(); 
      } 

      // get error output 
      pin = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
      String wout = null; 
      ptmp = pin.readLine(); 
      while (ptmp != null) { 
       wout = wout == null ? ptmp + '\n' : wout + ptmp + '\n'; 
       ptmp = pin.readLine(); 
      } 

      // print output 
      System.out.println(pout); 
      System.out.println(wout); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; // for test 
    } 

當這個服務被調用時,我總是得到一個錯誤:無法找到或加載主類:P [ID]

我的CD theFilePath時,P [id] .class文件已存在。 我可以在FilePath中成功運行java P [id]。

我嘗試運行時更換的ProcessBuilder,如:

@Service 
public class MRServiceImp implements MRService { 
    @Override 
    public String submitMR(int id, String fd) { 
     try { 
      // compile the java file 
      String[] cmd = {"javac", "P" + id + ".java"}; 
      ProcessBuilder pb = new ProcessBuilder(cmd); 
      pb.directory(new File(fd)); 
      Process p = pb.start(); 

      // exec the class file 
      String execmd = "java", fd + "/P" + pz_id; 
      p = Runtime.getRuntime().exec(execmd); 

      // get normal output 
      BufferedReader pin = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String ptmp = pin.readLine(); 
      while (ptmp != null) { 
       pout = pout == null ? ptmp + '\n' : pout + ptmp + '\n'; 
       ptmp = pin.readLine(); 
      } 

      // get error output 
      pin = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
      String wout = null; 
      ptmp = pin.readLine(); 
      while (ptmp != null) { 
       wout = wout == null ? ptmp + '\n' : wout + ptmp + '\n'; 
       ptmp = pin.readLine(); 
      } 

      // print output 
      System.out.println(pout); 
      System.out.println(wout); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; // for test 
    } 

我得到同樣的錯誤再次T^T

IDE是STS-包,服務器tomcat8

回答

1

我知道這裏有什麼問題。

pb.start();並不意味着pb的命令將立即執行。

所以如果我設置了命令javac hello.java的pb;設置epb的命令java你好

而我打電話pb.start(); epb.start();不斷,我會得到一個錯誤:無法找到或加載主類:你好,因爲當我執行epb.start();前一個命令(pb.start)可能尚未執行!

我得到了2解決方案:

首先:設置一個最後場和exec epb.start()在這一領域,如:

@Service 
public class MRServiceImp implements MRService { 
@Override 
public String submitMR(int id, String fd) { 
    try { 
     // compile the java file 
     String[] cmd = {"javac", "P" + id + ".java"}; 
     ProcessBuilder pb = new ProcessBuilder(cmd); 
     pb.directory(new File(fd)); 
     Process p = pb.start(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     // exec the class file 
     String[] execmd = {"java", "P" + pz_id}; 
     ProcessBuilder epb = new ProcessBuilder(execmd); 
     epb.directory(new File(fd)); 
     Process p = epb.start(); 
    } 
    return null; // for test 
} 

二:慶典的一招

@Service 
public class MRServiceImp implements MRService { 
@Override 
public String submitMR(int id, String fd) { 
    try { 
     // compile & exec the java file 
     String[] cmd = {"/bin/bash"}; 
     ProcessBuilder pb = new ProcessBuilder(cmd); 
     pb.directory(new File(fd)); 
     Process p = pb.start(); 
     BufferedWriter pbw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 
     pbw.write("javac *.java;java P" + pz_id+";exit;"); 
     pbw.newLine(); 
     pbw.flush(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; // for test 
} 

我使用第二個。

相關問題