在Java中实现“system”命令

时间:2009-09-02 20:10:44

标签: java javascript process system subprocess

我需要一个“系统”函数调用,与Python,Perl,PHP,Ruby和c中的调用相同。当它在Rhino JavaScript引擎上运行时,它将成为名为Narwhal的JavaScript标准库的一个组件,而该引擎又在Java上运行。

麻烦的是,Java的标准库似乎已经抽象出了生成共享父进程的stdio的子进程的能力。这意味着您无法将交互性推迟到子流程。

我的第一个问题是实现Python的subprocess.popen。这使用三个“pumper”线程独立地主动复制父进程的stdio(以防止死锁)。不幸的是,这给了我们两个问题。首先,当子进程自愿退出时,输入不会自动关闭。其次,子进程的流不会缓冲和正确刷新。

我正在寻找可以使我们的require(“os”)。system()命令按预期工作的解决方案。

该项目位于http://narwhaljs.org

相关代码:

4 个答案:

答案 0 :(得分:2)

不确定这是否是您要查找的内容,但您可以通过JNA library调用C system函数:

public class System {
  public interface C extends Library {
    C INSTANCE = (C) Native.loadLibrary(
        (Platform.isWindows() ? "msvcrt" : "c"), C.class);

    public int system(String format);
  }

  public static void main(String[] args) {
    C.INSTANCE.system("vi");
  }
}

无论如何,在Windows上进行粗略测试。

答案 1 :(得分:1)

您需要单独监视进程退出状态,方法是轮询退出代码或在Process.waitFor()方法中等待单独的线程。

关于缓冲和冲洗溪流的问题,我没有一个简单的解决方案。有几个Java类以不同的形式(BufferedInputStream等)进行缓冲。也许其中一个可以提供帮助?

答案 2 :(得分:1)

如果我正确理解你,你需要这样的东西:

import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }
    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }

    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                System.out.println(type + ">" + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}
public class GoodWinRedirect
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE java GoodWinRedirect <outputfile>");
            System.exit(1);
        }

        try
        {            
            FileOutputStream fos = new FileOutputStream(args[0]);
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("java jecho 'Hello World'");
            // any error message?
            StreamGobbler errorGobbler = new 
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUTPUT", fos);

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
            fos.flush();
            fos.close();        
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

我在前一段时间找到了这段代码:JavaWorld当我正在寻找类似的解决方案来将系统调用包装到某些exe文件时。

从那时起,我的代码发生了一些变化,但我认为这是一个很好的例子。

答案 3 :(得分:0)

同时使用流程标准输出和错误非常重要。请参阅Carlos Tasada的示例代码。

如果您不这样做,您的代码可能(或可能不)工作,具体取决于您生成的进程的输出。当输出发生变化时(如果您的衍生进程遇到错误,比如说),如果没有并发消耗,您的进程将会死锁。我在SO上看到的与Process.exec()相关的大多数问题都是阻塞相关的。