1

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
carlspring
  • 31,231
  • 29
  • 115
  • 197
saurabhk
  • 140
  • 1
  • 4
  • 14
  • I have detected the error :If i replace the du -h command with tasklist the the program works fine.The issue is Rumtime.getRuntime.exec() executes the command in the current runtime (as i am doing in windows) but i want to run the command in linux server and not in windows. – saurabhk Apr 29 '14 at 08:47
  • That will not give you the result you are looking for. You want to execute your command on the remote machine. Using `tasklist` gives you the result of your windows box, not your remote linux machine. – BetaRide Apr 29 '14 at 08:51

4 Answers4

1

I don't think that you are actually executing the du -h command on the remote server. Runtime.getRuntime() gets the Runtime instance on the local host. You should use a library that can telnet to the remote host and execute the command there, like http://commons.apache.org/proper/commons-net/

You can see here: http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html that this RunTime is getting the host envairorment and exec "du -h" at host computer and not on remote server

jdvr
  • 5
  • 3
0

When executing a command in Java, you're not in a classic shell. Hence you don't have access to the same environnement variables, built-in commands, etc. Try with /usr/bin/du

Grooveek
  • 10,046
  • 1
  • 27
  • 37
0

You cannot use Runtime.getRuntime().exec("du -h"); directly to execute something on a remote machine. The command is executed on the machine where the java programm has been started.

How do I run SSH commands on remote system using Java? answers your question.

Community
  • 1
  • 1
BetaRide
  • 16,207
  • 29
  • 99
  • 177
0

Try:

Runtime.getRuntime().exec("ssh ${hostname-goes-here} du -h /path/to/folder");

Of course, you will need to have ssh available on your path on the client's side and in order for the above command to work, you will need to have added your public key to the ~/.ssh/authorized_keys file.

carlspring
  • 31,231
  • 29
  • 115
  • 197