0

Here is the code. It is taking SampleProg.java as input. I am unable to provide the input for SampleProg.java. What I am looking for is to compile, run and give input for test cases for the program. I tried using Buffered Writer but I am unable to write into the input program.
It is getting stuck while giving input. How can i provide the input. I am getting stuck at the part where i am giving input and it is not processing further. I also tried to use ProcessBuilder, but i was stuck with the same issue.

CODE: Example.java

    public class Example {
    
        public static void main(String[] args) {
            try{
                String path = "src/main/java/SampleProg.java";
                compile(pathJava);
                Process runProcess = run();
                runProcess.waitFor();
                BufferedReader in = new BufferedReader(new 
                InputStreamReader(runProcess.getInputStream()));
                String s;
                Scanner sc = new Scanner(System.in);
                while((s = in.readLine()) != null){
                    System.out.println(s);
                    op.write(sc.nextInt());
                    op.close();
                      }
                    }
                catch(Exception e){
                e.printStackTrace();
                 }
               }
            private static void compile(String pathJava) throws 
            IOException, InterruptedException {
            String [] cmd = new String[4];
            cmd[0] = "javac";
            cmd[1] = "-d";
            cmd[2] = ".";
            cmd[3] = pathJava;
            Runtime r = Runtime.getRuntime();
            Process compileProcess = r.exec(cmd);
            compileProcess.waitFor();
            }
        
            private static Process run() throws IOException {
            String [] cmd = new String[2];
            cmd[0] = "java";
            cmd[1] = "SampleProg";
            Runtime r = Runtime.getRuntime();
            return r.exec(cmd);
          }
          }

3
  • Does this answer your question? Compiling and executing Java code using Runtime#exec() Commented Dec 1, 2021 at 16:32
  • You will get more helpful answers and comments if you indent your code properly. Properly formatted code is easier for people to read. Commented Dec 1, 2021 at 16:47
  • Still no solution. Commented Dec 2, 2021 at 10:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.