import
java.io.BufferedReader;
import
java.io.File;
import
java.io.IOException;
import
java.io.InputStreamReader;
public
class
GfgExecDemo {
public
static
void
main(String[] args) {
try
{
Process process1 = Runtime.getRuntime().exec(
"notepad.exe"
);
int
exitCode1 = process1.waitFor();
System.out.println(
"Variant 1 - Exit Code: "
+ exitCode1);
String[] cmd2 = {
"ls"
,
"-l"
,
"/path/to/directory"
};
Process process2 = Runtime.getRuntime().exec(cmd2);
int
exitCode2 = process2.waitFor();
System.out.println(
"Variant 2 - Exit Code: "
+ exitCode2);
String[] cmd3 = {
"echo"
,
"$HOME"
};
String[] env3 = {
"HOME=/path/to/custom/home"
};
Process process3 = Runtime.getRuntime().exec(cmd3, env3);
int
exitCode3 = process3.waitFor();
System.out.println(
"Variant 3 - Exit Code: "
+ exitCode3);
String[] cmd4 = {
"ls"
,
"-l"
};
String[] env4 = {
"HOME=/path/to/custom/home"
};
File workingDir4 =
new
File(
"/path/to/directory"
);
Process process4 = Runtime.getRuntime().exec(cmd4, env4, workingDir4);
int
exitCode4 = process4.waitFor();
System.out.println(
"Variant 4 - Exit Code: "
+ exitCode4);
String cmd5 =
"echo $HOME"
;
String[] env5 = {
"HOME=/path/to/custom/home"
};
Process process5 = Runtime.getRuntime().exec(cmd5, env5);
int
exitCode5 = process5.waitFor();
System.out.println(
"Variant 5 - Exit Code: "
+ exitCode5);
String cmd6 =
"ls -l"
;
String[] env6 = {
"HOME=/path/to/custom/home"
};
File workingDir6 =
new
File(
"/path/to/directory"
);
Process process6 = Runtime.getRuntime().exec(cmd6, env6, workingDir6);
int
exitCode6 = process6.waitFor();
System.out.println(
"Variant 6 - Exit Code: "
+ exitCode6);
}
catch
(IOException | InterruptedException e) {
e.printStackTrace();
}
}
}