Magazine
 
Tips n Tricks
 

CreateSubProcess.java:

public class CreateSubProcess
{
public static void main(String[] args) {
try {
//Get the current runtime to interface
with the environment in
//which the application is running.
Runtime runTime = Runtime.getRuntime();
System.out.println(“Solve the problem
within 1 minute.”);
Process process = runTime.exec(“calc”);
//Executes the specified string
// command in a separate process.
try {
Thread.sleep(60*1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
process.destroy();
// Kills the subprocess forcibly.
int exitValueCalc = process.exitValue();
//The exit value 0 indicates normal
termination.
System.out.println(“Process exitValue: “ +
exitValueCalc);
}
catch (Throwable e) {
e.printStackTrace();
}
}
}

Compile and Run:

C:\JavaJazzup>javac CreateSubProcess.java
C:\JavaJazzup>java CreateSubProcess

Solve the problem within 1 minute.

 

Output:

Running the above program prints the message “Solve the problem within 1 minute.” and opens a calculator.



8. Can you find the name of Operating System you are working on and environment variables by a Java program?

Environment variables are key/value pairs, which are available to all the programs running within DOS environment or top of it, including Windows. Because these can be changed so they are called variables. Java provides a way to access these variables by providing System class. To get the value of a single variable, call its static method getProperty(“property_name”). To get value of all the variables, use static method getenv() which returns values as a Map. Now using collection methods, keys and values can be displayed.

Sept 2007 | Java Jazz Up | 73
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF