saurabhk
saurabhk

Reputation: 140

Run du -f linux command and get the result in java

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

Upvotes: 1

Views: 1418

Answers (4)

carlspring
carlspring

Reputation: 32567

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.

Upvotes: 0

Lefteris Koutsoloukas
Lefteris Koutsoloukas

Reputation: 113

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

Upvotes: 1

BetaRide
BetaRide

Reputation: 16834

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.

Upvotes: 0

Grooveek
Grooveek

Reputation: 10094

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

Upvotes: 0

Related Questions