I have a scenario like this:
I need to connect to a remote server(linux) and execute du -h command to find the disk usage of a particular folder (say /home/oracle/TEST). So, i need to find the disk usage of TEST folder. and print the result in java. How can i do this?
public static void main(String[] args) {
String s = null;
try {
// run the Unix "ps -ef" command
Socket s1=new Socket("10.1.7.237",1521);
System.out.println(s1.isConnected());
System.out.println(s1.getRemoteSocketAddress());
System.out.println("connected");
System.out.println();
PrintWriter wr=new PrintWriter(new OutputStreamWriter(s1.getOutputStream()),true);
wr.println("Hi Server...");
wr.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(br.readLine());
Process p = Runtime.getRuntime().exec("du -h");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.out.println(e.getMessage());
System.exit(-1);
}
}
I have tried this also but got this exception:
Cannot run program "du": CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "du": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at testprog.main(testprog.java:27)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more