Question is to abstract.
I want to run java code using java but the thing is java code may take input from console using (Scanner or other methods...)
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println(i);
}
}
I tried running below program for this thing but it neither gives me error nor showing any output.
import java.util.Scanner;
import java.io.*;
public class MyExec {
public static void main(String[] args)
{
//init shell
ProcessBuilder builder = new ProcessBuilder( "/bin/bash" );
Process p=null;
try {
p = builder.start();
}
catch (IOException e) {
System.out.println(e);
}
//get stdin of shell
BufferedWriter p_stdin =
new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
try {
//single execution
p_stdin.write("javac Main.java");
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("java Main.class");
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("1");
p_stdin.newLine();
p_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
}
// finally close the shell by execution exit command
try {
p_stdin.write("exit");
p_stdin.newLine();
p_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
}
// write stdout of shell (=output of all commands)
Scanner s = new Scanner( p.getInputStream() );
while (s.hasNext())
{
System.out.println( s.next() );
}
s.close();
}
}
I want to pass inputs too How to figure the mistake out, Don't know what thing is going wrong here