cmd - Running compiled java code at runtime -


i want run code compiled before. compiled anyway not important how compile running code problem.

my code.java

public class code{      public static void main(string[] args) {         system.out.println("hello, world");     } } 

then compiled code , code.class(in d:// directory) generated. want run compiled file. code :

import java.io.ioexception; import java.io.inputstream;  public class compiler {    public static void main(string[] args) {       final string doscommand = "cmd /c java code";       final string location = "d:\\";       try {          final process process = runtime.getruntime().exec(             doscommand + " " + location);          final inputstream in = process.getinputstream();          int ch;          while((ch = in.read()) != -1) {             system.out.print((char)ch);          }       } catch (ioexception e) {          e.printstacktrace();       }    } } 

here there no error code not anything. no cmd opened, nothing. wrong? should do?

current cmd command wrong.

cmd /c java code d:/   /*this not correct cmd command*/ 

it should be

cmd /c java -cp d:/ code 

when run .class file in different folder not in current folder use -cp specifies class path

there no error nope there .but didn't capture them .to capture errors can use geterrorstream()

example code

public class compiler {      public static void main(string[] args) {         final string doscommand = "cmd /c java -cp ";         final string classname = "code";         final string location = "d:\\";         try {             final process process = runtime.getruntime().exec(doscommand + location + " " + classname);             final inputstream in = process.getinputstream();             final inputstream in2 = process.geterrorstream();             int ch, ch2;             while ((ch = in.read()) != -1) {                 system.out.print((char) ch);             }             while ((ch2 = in2.read()) != -1) {                 system.out.print((char) ch2); // read error here             }         } catch (ioexception e) {             e.printstacktrace();         }     } } 

Comments