I am writing a Java learning tool in Greenfoot. It's already working and I have this code.
to be called by these lines
Now, what I want to be able to do is, after the javac command, I'll run "java sample" inside a terminal.
I understand that going "cmd start java sample" only returns what is supposedly displayed on the cmd to the Greenfoot terminal. And by going "cmd start" ill just invoke a new cmd on the dir where my greenfoot project is at. (Since the target of our project is to teach Java to kids, I think it would be better if we need not include a documentation on how to manually run java on the console.)
I am on windows 7.
If anyone can point me to the right track or maybe give me a hint as to how ill be able to start my cmd already with java filename.. Please and Thank you
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static void runProc(String cmd) throws Exception{ Process proc = Runtime.getRuntime().exec(cmd); //executes terminal command received debug(cmd + ">" , proc.getInputStream()); debug(cmd + "[ERROR]" , proc.getErrorStream()); proc.waitFor(); //System.out.println(cmd + " exitValue(): " + proc.exitValue()); } public static void debug(String name, InputStream ins) throws Exception{ String line = null ; BufferedReader in = new BufferedReader( new InputStreamReader(ins)); while ((line = in.readLine()) != null ){ //System.out.println(name + " " + line); System.out.println(line); } } |
1 2 3 4 5 | try { runProc( "javac sample.java" ); runProc( "java sample" ); } catch (Throwable t){ t.printStackTrace();} |