【问题标题】:Implementing the "system" command in Java在 Java 中实现“系统”命令
【发布时间】:2010-11-25 02:04:54
【问题描述】:

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

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

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

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

项目位于http://narwhaljs.org

相关代码:

【问题讨论】:

    标签: java javascript process system subprocess


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

        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 文件的系统调用。

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

        【讨论】:

          【解决方案4】:

          不确定这是否是您要查找的内容,但您可以通过 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 上运行。

          【讨论】:

          • 添加 jni.jar,这部分 JavaScript 就成功了。谢谢! var jna = Packages.com.sun.jna; var clib = jna.NativeLibrary.getInstance(jna.Platform.isWindows() ? "msvcrt" : "c"); var csystem = clib.getFunction("系统"); csystem.invoke(["echo Hello, World!"]); gist.github.com/181225
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-03
          • 1970-01-01
          • 1970-01-01
          • 2018-05-02
          • 2018-09-18
          • 1970-01-01
          相关资源
          最近更新 更多