<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FDevelopment%2FProcess</id>
		<title>Java Tutorial/Development/Process - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FDevelopment%2FProcess"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Development/Process&amp;action=history"/>
		<updated>2026-04-21T22:39:54Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Development/Process&amp;diff=3737&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Development/Process&amp;diff=3737&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/Development/Process&amp;diff=3738&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/Development/Process&amp;diff=3738&amp;oldid=prev"/>
				<updated>2010-05-31T15:30:42Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Execute external command and obtain the result ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.BufferedReader;&lt;br /&gt;
import java.io.InputStreamReader;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] args) throws Exception{&lt;br /&gt;
    Process process = Runtime.getRuntime().exec(&amp;quot;ls -al&amp;quot;);&lt;br /&gt;
    process.waitFor();&lt;br /&gt;
    int exitValue = process.exitValue();&lt;br /&gt;
    System.out.println(&amp;quot;exitValue = &amp;quot; + exitValue);&lt;br /&gt;
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));&lt;br /&gt;
    String line = &amp;quot;&amp;quot;;&lt;br /&gt;
    while ((line = reader.readLine()) != null) {&lt;br /&gt;
      System.out.println(line);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Helper method to execute shell command ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1998-2002 Carnegie Mellon University.  All rights&lt;br /&gt;
 * reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in&lt;br /&gt;
 *    the documentation and/or other materials provided with the&lt;br /&gt;
 *    distribution.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS&amp;quot;&amp;quot; AND&lt;br /&gt;
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY&lt;br /&gt;
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,&lt;br /&gt;
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,&lt;br /&gt;
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY&lt;br /&gt;
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT&lt;br /&gt;
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE&lt;br /&gt;
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.io.*;&lt;br /&gt;
public abstract class Exec {&lt;br /&gt;
&lt;br /&gt;
    public static Process exec (String[] cmdarray) throws IOException {&lt;br /&gt;
        return exec (cmdarray, null, null);&lt;br /&gt;
    }&lt;br /&gt;
    public static Process exec (String[] cmdarray, String[] envp) throws IOException {&lt;br /&gt;
        return exec (cmdarray, envp, null);&lt;br /&gt;
    }&lt;br /&gt;
    public static Process exec (String[] cmdarray, String[] envp, File directory) throws IOException {&lt;br /&gt;
  return &lt;br /&gt;
      isWindows ()&lt;br /&gt;
      ? execWindows (cmdarray, envp, directory)&lt;br /&gt;
      : execUnix (cmdarray, envp, directory);&lt;br /&gt;
    } &lt;br /&gt;
    /*&lt;br /&gt;
     * Unix&lt;br /&gt;
     */&lt;br /&gt;
    static Process execUnix (String[] cmdarray, String[] envp, File directory) throws IOException {&lt;br /&gt;
        // instead of calling command directly, we&amp;quot;ll call the shell to change&lt;br /&gt;
        // directory and set environment variables.&lt;br /&gt;
        // start constructing the sh command line.&lt;br /&gt;
        StringBuffer buf = new StringBuffer ();&lt;br /&gt;
        if (directory != null) {&lt;br /&gt;
            // change to directory&lt;br /&gt;
            buf.append (&amp;quot;cd &amp;quot;&amp;quot;);&lt;br /&gt;
            buf.append (escapeQuote (directory.toString ()));&lt;br /&gt;
            buf.append (&amp;quot;&amp;quot;; &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        if (envp != null) {&lt;br /&gt;
            // set environment variables.  Quote the value (but not the name).&lt;br /&gt;
            for (int i = 0; i &amp;lt; envp.length; ++i) {&lt;br /&gt;
                String nameval = envp[i];&lt;br /&gt;
                int equals = nameval.indexOf (&amp;quot;=&amp;quot;);&lt;br /&gt;
                if (equals == -1)&lt;br /&gt;
                    throw new IOException (&amp;quot;environment variable &amp;quot;&amp;quot; + nameval &lt;br /&gt;
                                           + &amp;quot;&amp;quot; should have form NAME=VALUE&amp;quot;);&lt;br /&gt;
                buf.append (nameval.substring (0, equals+1));&lt;br /&gt;
                buf.append (&amp;quot;\&amp;quot;&amp;quot;);&lt;br /&gt;
                buf.append (escapeQuote (nameval.substring (equals+1)));&lt;br /&gt;
                buf.append (&amp;quot;\&amp;quot; &amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // now that we have the directory and environment, run &amp;quot;which&amp;quot; &lt;br /&gt;
        // to test if the command name is found somewhere in the path.&lt;br /&gt;
        // If &amp;quot;which&amp;quot; fails, throw an IOException.&lt;br /&gt;
        String cmdname = escapeQuote (cmdarray[0]); &lt;br /&gt;
        Runtime rt = Runtime.getRuntime ();&lt;br /&gt;
        String[] sharray = new String[] { &amp;quot;sh&amp;quot;, &amp;quot;-c&amp;quot;, buf.toString () + &amp;quot; which \&amp;quot;&amp;quot; + cmdname + &amp;quot;\&amp;quot;&amp;quot; };&lt;br /&gt;
        Process which = rt.exec (sharray);&lt;br /&gt;
        try {&lt;br /&gt;
            which.waitFor ();&lt;br /&gt;
        } catch (InterruptedException e) {&lt;br /&gt;
            throw new IOException (&amp;quot;interrupted&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        if (which.exitValue () != 0) &lt;br /&gt;
            throw new IOException (&amp;quot;can&amp;quot;t execute &amp;quot; + cmdname + &amp;quot;: bad command or filename&amp;quot;); &lt;br /&gt;
        // finish in &lt;br /&gt;
        buf.append (&amp;quot;exec \&amp;quot;&amp;quot;);&lt;br /&gt;
        buf.append (cmdname);&lt;br /&gt;
        buf.append (&amp;quot;\&amp;quot; &amp;quot;);&lt;br /&gt;
        // quote each argument in the command&lt;br /&gt;
        for (int i = 1; i &amp;lt; cmdarray.length; ++i) {&lt;br /&gt;
            buf.append (&amp;quot;\&amp;quot;&amp;quot;);&lt;br /&gt;
            buf.append (escapeQuote (cmdarray[i]));&lt;br /&gt;
            buf.append (&amp;quot;\&amp;quot; &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println (&amp;quot;executing &amp;quot; + buf);&lt;br /&gt;
        sharray[2] = buf.toString ();&lt;br /&gt;
        return rt.exec (sharray);&lt;br /&gt;
    }&lt;br /&gt;
    static String escapeQuote (String s) {&lt;br /&gt;
        // replace single quotes with a bit of magic (end-quote, escaped-quote, start-quote) &lt;br /&gt;
        // that works in a single-quoted string in the Unix shell&lt;br /&gt;
        if (s.indexOf (&amp;quot;\&amp;quot;&amp;quot;) != -1) {&lt;br /&gt;
          System.out.println (&amp;quot;replacing single-quotes in &amp;quot; + s);&lt;br /&gt;
            s = s.replace(&amp;quot;&amp;quot;&amp;quot;, &amp;quot;&amp;quot;\\&amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
            System.out.println (&amp;quot;to get &amp;quot; + s);&lt;br /&gt;
        }&lt;br /&gt;
        return s;&lt;br /&gt;
    }&lt;br /&gt;
    /*&lt;br /&gt;
     * Windows&lt;br /&gt;
     */&lt;br /&gt;
     static boolean isWindows () {&lt;br /&gt;
        String os = System.getProperty (&amp;quot;os.name&amp;quot;);&lt;br /&gt;
  return (os != null &amp;amp;&amp;amp; os.startsWith (&amp;quot;Windows&amp;quot;));&lt;br /&gt;
     }&lt;br /&gt;
     static boolean isJview () {&lt;br /&gt;
        String vendor = System.getProperty (&amp;quot;java.vendor&amp;quot;);&lt;br /&gt;
  return (vendor != null &amp;amp;&amp;amp; vendor.startsWith (&amp;quot;Microsoft&amp;quot;));&lt;br /&gt;
     }&lt;br /&gt;
    static Process execWindows (String[] cmdarray, String[] envp, File directory) throws IOException {&lt;br /&gt;
  if (envp != null || directory != null) {&lt;br /&gt;
      if (isJview ())&lt;br /&gt;
    // jview doesn&amp;quot;t support JNI, so can&amp;quot;t call putenv/chdir&lt;br /&gt;
    throw new IOException &lt;br /&gt;
        (&amp;quot;can&amp;quot;t use Exec.exec() under Microsoft JVM&amp;quot;);&lt;br /&gt;
      &lt;br /&gt;
      if (!linked) {&lt;br /&gt;
    try {&lt;br /&gt;
        System.loadLibrary (&amp;quot;win32exec&amp;quot;);&lt;br /&gt;
        linked = true;&lt;br /&gt;
    } catch (LinkageError e) {&lt;br /&gt;
        throw new IOException (&amp;quot;can&amp;quot;t use Exec.exec(): &amp;quot;&lt;br /&gt;
             + e.getMessage ());&lt;br /&gt;
    }&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      if (envp != null) {&lt;br /&gt;
    for (int i = 0; i &amp;lt; envp.length; ++i)&lt;br /&gt;
        putenv (envp[i]);&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      if (directory != null)&lt;br /&gt;
    chdir (directory.toString ());&lt;br /&gt;
  }&lt;br /&gt;
        return Runtime.getRuntime ().exec (cmdarray);&lt;br /&gt;
    }&lt;br /&gt;
    static boolean linked = false; // true after System.loadLibrary() is called&lt;br /&gt;
    static native boolean putenv (String env);&lt;br /&gt;
    static native boolean chdir (String dir);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Reading Output from a Command ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    String command = &amp;quot;ls&amp;quot;;&lt;br /&gt;
    Process child = Runtime.getRuntime().exec(command);&lt;br /&gt;
    InputStream in = child.getInputStream();&lt;br /&gt;
    int c;&lt;br /&gt;
    while ((c = in.read()) != -1) {&lt;br /&gt;
      System.out.println(((char) c));&lt;br /&gt;
    }&lt;br /&gt;
    in.close();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Sending Input to a Command ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.OutputStream;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    String command = &amp;quot;cat&amp;quot;;&lt;br /&gt;
    Process child = Runtime.getRuntime().exec(command);&lt;br /&gt;
    OutputStream out = child.getOutputStream();&lt;br /&gt;
    out.write(&amp;quot;some text&amp;quot;.getBytes());&lt;br /&gt;
    out.close();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>