我有以下代码来运行三次执行:
public static void main(String[] args) throws InterruptedException, IOException
{
String filepath1 = "cmd /c gradlew jmhJar";
String filepath2 = "cmd /c java -jar path/to/the/file/filename.jar -rf csv -rff path/to/save/file1.csv -wi 3 -i 5 -f 2";
String filepath4 = "cmd /c javac path/to/the/file/ParserHash.java";/*Code to compile is parserHash.java*/
String filepath3 = "cmd /c java path/to/the/compiled/class/ParserHash "C:/Users/msek/Desktop/trial/result/file1.csv C:/Users/msek/Desktop/trial/result/file2.csv C:/Users/msek/Desktop/trial/result/file3.csv";
try
{
runProcess(filepath1);
runProcess(filepath2);
System.out.println("Sucessfully written into file1.csv");
runProcess(filepath4);
System.out.println("Compilation Over");
runProcess(filepath3);
System.out.println("Program Sucessfully Executed");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void runProcess(String processString)
{
try
{
final Process p = Runtime.getRuntime().exec(processString);
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
}
catch (Exception x)
{
x.printStackTrace();
}
}
如果我编译java文件到那个特定的目录并成功编译它的编译并运行它,它会成功执行。但是,如果我将它传递给“cmd / c path / to / java / file / file.java”,它会被编译,但是当我执行它时,我得到一个错误,指出无法找到或加载mainclass eventHough类文件存在。< / p>
我看了很多关于这个建议的建议,但这没有用。
我只是想知道我哪里出错了以及如何编译,通过使用Runtime.exec()传递多个参数来执行java文件。
答案 0 :(得分:1)
java path/to/the/compiled/class/ParserHash
如果您遇到exec()
问题,应该:
java
命令的参数不是路径,而是类名,完全限定,即包括包名。带点。