package src;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Command
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("cmd /c dir");
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
System.out.println("Done");
}
}
I tried to run this code but it is not able to execute anything. Are there some classpath or other settings i need to do to make this work?
asked Jul 2, 2012 at 8:28
agarwav
4002 gold badges8 silver badges16 bronze badges
1 Answer 1
answered Jul 2, 2012 at 8:42
Nir Alfasi
53.6k12 gold badges94 silver badges138 bronze badges
Sign up to request clarification or add additional context in comments.
11 Comments
agarwav
ok and alfasin what exactly would i have to write if i want to run a java program suppose a.java? What do i have to change?
Nir Alfasi
javac a.java and then java a (from prompt)agarwav
yeah but i want that this all should be done by the program which i have posted. So is there a way that this program automatically compiles and runs the program?
Nir Alfasi
what do you mean by "automatically" ? scheduled task (cron) ?
agarwav
no, i mean that i will run this java program which will automatically compile another java program and run it. My main aim is that i don't want to go to terminal to compile and run a java program. I want to run commands via this java program.
|
lang-java
e1.printStrackTrace();in the exception blocks to see whats wrong>javac Command.java -d .and Run it from command prompt>java src.CommandreadLine(), waiting for input or waiting at thewaitFor(). Related question was answered here: stackoverflow.com/questions/8149828/…, it has thewaitFor()after the loop