0

I am trying to call my rrdtool cmd from a java class, not sure how to go about it.

I have tested my RRDTool cmd from my terminal and it is successful, see below.

rrdtool update mydb.rrd 1385056701:6:5

How do i execute this cmd from a java class?

Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • 1
    See : [how-to-run-linux-commands-in-java-code](http://stackoverflow.com/questions/3403226/how-to-run-linux-commands-in-java-code) – abronan Nov 21 '13 at 18:16

6 Answers6

2

You can use the below command format to run your Linux command.

Runtime r = Runtime.getRuntime();
Process p = r.exec(yourcmd);

Please go through Running unix command from Java and Unable to run Unix command in Java-Stackoverflow

Hope you get your answers here.

Community
  • 1
  • 1
Vishal R
  • 1,026
  • 1
  • 7
  • 21
1

try this

        public class ShellTest {
    public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
        // Get runtime
        java.lang.Runtime rt = java.lang.Runtime.getRuntime();
        // Start a new process: UNIX command ls
        java.lang.Process p = rt.exec("ls");
        // Show exit code of process
        System.out.println("Process exited with code = " + rt.exitValue());
    }
}

also check here for more details

Freaky Thommi
  • 756
  • 7
  • 18
0

You can use Runtime#exec for this purpose. If you want a Java-like control over the started process, there is a great library called zt-exec that makes handeling processes much easier. The ProcessBuilder is also offering a minor API improvement over Runtime#exec that ships with the Java standard library.

Something you have to take care of is that Java processes come with very little buffer for the in- and output streams what blocks processes once these buffers run full. This happens silently. zt-exec can help you with that.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
0

Try like this(As answered by paxdiablo):

public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }

Also check java.lang.Runtime.exec for details.

Executes the specified string command in a separate process.

This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You can have a look at : https://github.com/OpenNMS/opennms/blob/master/opennms-rrd/opennms-rrd-rrdtool/opennms-rrdtool-api/src/main/java/org/opennms/netmgt/rrd/rrdtool/JniRrdStrategy.java

for a real life use of rrdtool in java. Or at https://code.google.com/p/rrd4j/

For a native version of rrdtool in java.

fbacchella
  • 223
  • 1
  • 8
0

I runned my rrdtool command in java program as follow:

    Process p = null;
    try {

        ProcessBuilder pb = new ProcessBuilder("/usr/bin/rrdtool","lastupdate", rrdPath);

        pb.redirectErrorStream(true);
        p = pb.start();
        int exitVal = p.waitFor();
        if (exitVal == 0)
            System.out.println("exitVal of rrdLastUpdate is Successful");
        else
            System.out.println("exitVal of rrdLastUpdate is Abnormal");

    } catch (Exception e) {
        System.out.println("Problem in executing rrdlastupdate()");
        e.printStackTrace();

    }//end of try-catch

I hope this be useful for U :) I worked with some other rrdtool commands in java. if you need more help, I will be happy to help.

masoumeh
  • 468
  • 1
  • 5
  • 15