Javaファイルをコンパイルし、そのクラスを別のクラスでexecしたい(←このクラスは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]
I CD theFilePath、P [id] .classファイルは存在します。 そして、私はtheFilePathでjava P [id]を正常に実行できます。
そして、私は次のように、ランタイムでProcessBuilderをを交換してみてください。再びT^T
IDEは、STS-バンドルされ、サーバは私が知っている