我可以使用以下示例代码成功编译任何java类:
static String testFileName = "OOJavaBootcampTest";
System.out.println("Compiling student test source file");
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";
System.out.println("Compile student source command: "
+ compileCommand);
try {
Runtime.getRuntime().exec(compileCommand).
waitFor(5000, TimeUnit.MILLISECONDS);
} catch (IOException | InterruptedException ex) {
}
这可以正常工作,但使用相同的代码编译Junit测试类,但添加了jUnit类路径:
final static String jUnitFileName = "junit-4.11.jar";
...
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
+ "\"C:\\submissions\\SOFT222\\" + jUnitFileName
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";
test.java文件似乎无法复制到-d中的指定目录。这是compilerOutput文件中的错误:
javac: invalid flag: C:\submissions\SOFT222\junit-4.11.jar
Usage: javac <options> <source files>
use -help for a list of possible options
Junit肯定在指定的位置。
答案 0 :(得分:0)
类路径条目需要用;
(在Windows上)
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
+ "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + ";"
+ "\"C:\\submissions\\SOFT222\\" + jUnitFileName
+ "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
+ testFileName + ".java\" >> \"" + ""
+ "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1"