Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

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).

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top