You should use the Runtime class to do so.
code file --x.java
to execute-y.java
Now you should be using the "exec" function of Runtime class.
like this
Process p=Runtime.getRuntime().exec("cmd /c javac y.java && java y.java");
so now the program executes from.a process made by this class.
This method returns a process
so in order to receive the output of this command just executed do like this
p.getOutputStream()
// wrap this outputStream in a outputstream wtiter or may be a Printwriter and read it till its not null
and finally output it.