<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Java_System_Calls</id>
	<title>Java System Calls - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.scott5.org/index.php?action=history&amp;feed=atom&amp;title=Java_System_Calls"/>
	<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Java_System_Calls&amp;action=history"/>
	<updated>2026-04-13T00:24:44Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://wiki.scott5.org/index.php?title=Java_System_Calls&amp;diff=161&amp;oldid=prev</id>
		<title>Scott: /* booleanCommand */</title>
		<link rel="alternate" type="text/html" href="https://wiki.scott5.org/index.php?title=Java_System_Calls&amp;diff=161&amp;oldid=prev"/>
		<updated>2011-01-31T22:07:36Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;booleanCommand&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In Unix,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/bin/sh -c &amp;lt;commands&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
is a way to ask the shell to process &amp;lt;commands&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In Windows, you may need&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
command \c dir&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cmd \c dir&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
to execute DOS shell commands like &amp;lt;code&amp;gt;dir&amp;lt;/code&amp;gt;. Otherwise, the command name should suffice. &lt;br /&gt;
&lt;br /&gt;
==StreamGobbler==&lt;br /&gt;
See http://hacks.oreilly.com/pub/h/1092 for a good description of this &lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class StreamGobbler extends Thread {&lt;br /&gt;
  private InputStream is;&lt;br /&gt;
  private StringBuffer buffer;&lt;br /&gt;
&lt;br /&gt;
  StreamGobbler(InputStream is, StringBuffer buffer) {&lt;br /&gt;
    this.is = is;&lt;br /&gt;
    this.buffer = buffer;&lt;br /&gt;
    this.start();&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
  StreamGobbler(InputStream is) {&lt;br /&gt;
    this(is, null);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void run() {&lt;br /&gt;
    int nextChar;&lt;br /&gt;
    if (buffer == null) {&lt;br /&gt;
      try {&lt;br /&gt;
        while ((nextChar = is.read()) != -1) {} // do nothing&lt;br /&gt;
      } catch (IOException ioe) {&lt;br /&gt;
        ioe.printStackTrace();&lt;br /&gt;
      }&lt;br /&gt;
    } else {&lt;br /&gt;
      try {&lt;br /&gt;
        while ((nextChar = is.read()) != -1) {&lt;br /&gt;
          buffer.append((char) nextChar);&lt;br /&gt;
        }&lt;br /&gt;
      } catch (IOException ioe) {&lt;br /&gt;
        ioe.printStackTrace();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==booleanCommand==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public static boolean booleanCommand(String command) {&lt;br /&gt;
  Runtime rt = Runtime.getRuntime();&lt;br /&gt;
  String[] callAndArgs = {&amp;quot;/bin/bash&amp;quot;, &amp;quot;-c&amp;quot;, command};&lt;br /&gt;
  int exitValue = Integer.MIN_VALUE;&lt;br /&gt;
  Process child = null;&lt;br /&gt;
  try {&lt;br /&gt;
    child = rt.exec(callAndArgs);&lt;br /&gt;
    StreamGobbler errorGobbler = new StreamGobbler(child.getErrorStream());&lt;br /&gt;
    StreamGobbler inputGobbler = new StreamGobbler(child.getInputStream());&lt;br /&gt;
    exitValue = child.waitFor();&lt;br /&gt;
  } catch (IOException e) {&lt;br /&gt;
    logger.error(&amp;quot;IOException starting process!&amp;quot;, e);&lt;br /&gt;
  } catch (InterruptedException e) {&lt;br /&gt;
    logger.error(&amp;quot;Interrupted waiting for process!&amp;quot;, e);&lt;br /&gt;
  }&lt;br /&gt;
  if (exitValue == Integer.MIN_VALUE) {&lt;br /&gt;
    logger.error(&amp;quot;boolean command &amp;quot; + command + &amp;quot; failed to execute!&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  return (exitValue == 0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==stringCommandIgnoreReturnValue==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public static String stringCommandIgnoreReturnValue(String command) {&lt;br /&gt;
  Runtime rt = Runtime.getRuntime();&lt;br /&gt;
  String[] callAndArgs = {&amp;quot;/bin/bash&amp;quot;, &amp;quot;-c&amp;quot;, command};&lt;br /&gt;
  StringBuffer inputBuffer = new StringBuffer();&lt;br /&gt;
  int exitValue = Integer.MIN_VALUE;&lt;br /&gt;
  Process child = null;&lt;br /&gt;
  try {&lt;br /&gt;
    child = rt.exec(callAndArgs);&lt;br /&gt;
    StreamGobbler errorGobbler = new StreamGobbler(child.getErrorStream());&lt;br /&gt;
    StreamGobbler inputGobbler = new StreamGobbler(child.getInputStream(), inputBuffer);&lt;br /&gt;
    exitValue = child.waitFor();&lt;br /&gt;
  } catch (IOException e) {&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
    System.err.println(&amp;quot;IOException starting process!&amp;quot;);&lt;br /&gt;
  } catch (InterruptedException e) {&lt;br /&gt;
    System.err.println(&amp;quot;Interrupted waiting for process!&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  if (exitValue == Integer.MIN_VALUE) {&lt;br /&gt;
    throw new IllegalStateException(&amp;quot;command &amp;quot; + command + &amp;quot; failed to return any output!&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
  return inputBuffer.toString();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Scott</name></author>
	</entry>
</feed>