Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Call <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29" rel="nofollow"><code>Runtime.exec(String)</code></a> where the argument is the path to the "hostid" executable then, drain both streams of the resulting <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html" rel="nofollow"><code>Process</code> object</a> and take the contents of the standard output stream as your string value.</p> <p>This simple class demonstrates how you could implement this strategy (but needs improvement for error handling [e.g. stderr, exceptions] and OOP best-practices [e.g. returning an object with bean properties, etc.]):</p> <pre><code>public class RunCommand { public static String exec(String command) throws Exception { Process p = Runtime.getRuntime().exec(command); String stdout = drain(p.getInputStream()); String stderr = drain(p.getErrorStream()); return stdout; // TODO: return stderr also... } private static String drain(InputStream in) throws IOException { int b = -1; StringBuilder buf = new StringBuilder(); while ((b=in.read()) != -1) buf.append((char) b); return buf.toString(); } } </code></pre> <p>Your program could use it as such:</p> <pre><code>String myHostId = RunCommand.exec("/usr/bin/hostid").trim(); </code></pre> <p>Note that using a <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html" rel="nofollow"><code>ProcessBuilder</code></a> to create the <code>Process</code> might be more appropriate than <code>Runtime.exec()</code> if your command needs arguments or an environment, etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload