-1

I have the following code to run three executions :

            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();
    }
}

If I compile the java file going to that particular directory and compile its compiling successfully and on running it, it executes successfully. But if I pass it like "cmd /c path/to/java/file/file.java" its getting compiled but when I execute it, I get an error stating that could not find or load mainclass eventhough class file is present.

I have looked various links on this which suggested buid process, but that didn't work.

I just want to know where I'm going wrong and how to compile, execute a java file by passing multiple arguments using Runtime.exec()..

nt fury
  • 39
  • 1
  • 9

1 Answers1

1
java path/to/the/compiled/class/ParserHash

If you're having trouble with an exec() you should:

  1. Try the command from a command line yourself. It will fail the same way in this case.
  2. Look up the syntax of the command. In this case you will learn that the argument to the java command is not a path but a class name, fully qualified, i.e. including the package name. With dots.
user207421
  • 305,947
  • 44
  • 307
  • 483