Java 使用Runtime.exec()编译和执行时无法加载主类

Java 使用Runtime.exec()编译和执行时无法加载主类,java,processbuilder,runtime.exec,Java,Processbuilder,Runtime.exec,我有以下代码来运行三次执行: public static void main(String[] args) throws InterruptedException, IOException { String filepath1 = "cmd /c gradlew jmhJar"; String filepath2 = "cmd /c java -jar path/to/the/fil

我有以下代码来运行三次执行:

            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/cpath/to/java/file/file.java”一样传递它,它会被编译,但当我执行它时,我会收到一个错误,说明在存在类文件的情况下无法找到或加载mainclass Event

我已经查看了关于这个建议buid流程的各种链接,但都不起作用

我只想知道哪里出了问题,以及如何通过使用Runtime.exec()传递多个参数来编译、执行java文件

如果您在使用
exec()
时遇到问题,您应该:

  • 请自己从命令行尝试该命令。在这种情况下,它将以同样的方式失败
  • 查找命令的语法。在本例中,您将了解到
    java
    命令的参数不是路径,而是完全限定的类名,即包括包名。有点

  • javac
    之后,您是否构建了
    .jar
    文件?向您添加一个类路径command@UsagiMiyamoto实际上,前两个进程运行良好,没有任何问题,问题从javafile的编译部分开始,即filepath 3和filepath 4编译执行。
    java path/to/the/compiled/class/ParserHash