4

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

问题在于,Java 的标准库似乎已经抽象出生成共享父进程标准输入输出的子进程的能力。这意味着您不能将交互性推迟到子流程。

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

我正在寻找能够使我们的 require("os").system() 命令按预期工作的解决方案。

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

相关代码:

4

4 回答 4

2

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

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 上工作。

于 2009-09-02T22:08:27.667 回答
1

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

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

于 2009-09-02T20:20:19.117 回答
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 文件的系统调用。

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

于 2009-09-03T08:39:48.557 回答
0

同时消费过程标准输出和错误非常重要。请参阅此处其他地方的 Carlos Tasada 的示例代码。

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

于 2009-09-03T10:10:41.207 回答