我编写了一个代码,用于编译和执行其他程序,如C ++,C或Java。编译和执行C ++文件已成功实现。我编写的程序可以编译java程序(这就是我为编译ProjectDetailQuantity
创建ProcessBuilder的方法)。但问题是关于java代码执行。我创建了像
processBuilder = new ProcessBuilder("javac", fileName);
其中processBuilder = new ProcessBuilder("java", fileName);
是哪个.class文件将被执行。然后我设置了工作目录,inputfile路径和outputFilePath。但是fileName
是非零的,即1.在我的情况下是exit code
Bellow我已经提供了我写的执行方法。
RUN_TIME_ERROR
我正在尝试执行一个非常简单的程序。
public CompileStatus execute(ProcessBuilder processBuilder, String fileLocation,
String inputFilePath, String outputFilePath,
long timeInMillis) {
processBuilder.redirectErrorStream(true);
processBuilder.directory(new File(fileLocation));
processBuilder.redirectInput(new File(inputFilePath));
processBuilder.redirectOutput(new File(outputFilePath));
try {
Process process = processBuilder.start();
if (!process.waitFor(timeInMillis, TimeUnit.MILLISECONDS))
return CompileStatus.TIME_LIMIT_EXIT;
int exitCode = process.exitValue();
if (exitCode != 0) {
logger.info("Exit code {}", exitCode);
return CompileStatus.RUN_TIME_ERROR;
}
} catch (Exception e) {
logger.info("{}", e);
return CompileStatus.EXECUTION_ERROR;
}
return CompileStatus.EXECUTION_SUCCESS;
}
我在这里缺少什么?
====== UPDATE ==
public class C {
public static final void main(String... args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
System.out.println(i);
}
}
包含文本文件路径,该路径将作为java程序inputFilePath
的输入。并且C
包含文本文件路径,其中包含程序outputFilePath
的输出。
如果我在C
指示的文件中放入1,则程序inputFilePath
的输出将存储在C
指示的文件中。该文件应该包含1.但此时文件中没有任何内容由outputFilePath
指示,因为执行时出错。我检查了错误输入流但没有任何内容。但退出代码是程序outputFilePath
是1.
为了方便起见,我把日志放了。
C
[http-nio-8080-exec-4] INFO com.broj.controller.CompileRestController - userName loti, srcType java, srcFile /home/seal/test/C.java, inputFile /home/seal/test/in1.txt, outputFile Test
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - Code compilation started...
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - file location, /home/seal/test
[http-nio-8080-exec-4] INFO com.broj.service.CompileImpl - COMPILE_SUCCESS
[http-nio-8080-exec-4] INFO com.broj.service.engine.ProcessBuilderFactory - Language java
[http-nio-8080-exec-4] INFO com.broj.service.engine.ProcessBuilderFactory - File name C
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - Execution started
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - file location, /home/seal/test
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - input file path, /home/seal/test/in1.txt
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - output file path /home/seal/test/out.txt
[http-nio-8080-exec-4] INFO com.broj.service.engine.CompilerImpl - Exit code 1
[http-nio-8080-exec-4] INFO com.broj.service.CompileImpl - Execution status RUN_TIME_ERROR
工厂类。
ProcessBuilder
======更新==
输出文件的内容
public class ProcessBuilderFactory {
private static final Logger logger = LoggerFactory.getLogger(ProcessBuilderFactory.class);
public static ProcessBuilder getProcessBuilderForCompile(String language, String fileName, String targetFileName) {
ProcessBuilder processBuilder = null;
if (Language.CPP.equalsIgnoreCase(language))
processBuilder = new ProcessBuilder("g++", fileName, "-o", targetFileName);//not support c++11 so far
else if (Language.JAVA.equalsIgnoreCase(language))
processBuilder = new ProcessBuilder("javac", fileName);
return processBuilder;
}
public static ProcessBuilder getProcessBuilderForExecution(String language, String fileName) {
logger.info("Language {}", language);
logger.info("File name {}", fileName);
ProcessBuilder processBuilder = null;
if (Language.CPP.equalsIgnoreCase(language))
processBuilder = new ProcessBuilder("./" + fileName);
else if (Language.JAVA.equalsIgnoreCase(language))
processBuilder = new ProcessBuilder("java", fileName);
return processBuilder;
}
}