Home >Java >javaTutorial >How Can I Execute Command Lines in Java?
Executing Command Lines in Java
Java applications often need to interact with external processes and execute commands in the operating system's shell. This can be achieved using the Runtime class.
To execute the command line:
java -jar map.jar time.rel test.txt debug
within a Java application:
Get an instance of Runtime:
Runtime rt = Runtime.getRuntime();
Use the exec() method to execute the command:
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");
This creates a new process to execute the specified command. The exec() method returns an instance of Process, which allows you to handle the process, such as waiting for its completion or reading its output.
You can find more detailed information about the Runtime class at:
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
The above is the detailed content of How Can I Execute Command Lines in Java?. For more information, please follow other related articles on the PHP Chinese website!