实施“制度” Java 中的命令

发布于 2024-08-03 05:41:58 字数 1075 浏览 2 评论 0原文

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

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

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

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

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

相关代码:

I have need for a "system" function call, the same as those in Python, Perl, PHP, Ruby, &c. It will be a component of a JavaScript standard library called Narwhal, when it's run on the Rhino JavaScript engine, which is in turn run on Java.

The trouble is that Java's standard library appears to have abstracted away the ability to spawn a subprocess that shares the parent process's stdio. This means that you can't defer interactivity to the subprocess.

My first crack at this was to implement Python's subprocess.popen. This uses three "pumper" threads to actively copy the parent process's stdio independently (to prevent deadlock). Unfortunately this is giving us two problems. First, the input does not close automatically when the sub-process voluntarily exits. Second, the streams to the child process do not buffer and flush properly.

I'm looking for solutions that would make our require("os").system() command work as one would expect.

The project is at http://narwhaljs.org

Relevant code:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

⊕婉儿 2024-08-10 05:41:58

不确定这是否是您正在寻找的,但您可以通过 JNA 库

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 上是有效的。

Not sure if this is what you're looking for, but you can invoke the C system function through the JNA library:

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");
  }
}

Cursory testing worked on Windows, anyhow.

山川志 2024-08-10 05:41:58

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

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

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

If I'm understanding you properly, you want something like that:

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();
          }
    }
}

I found this piece of code in: JavaWorld sometime ago when I was looking for a similar solution to wrap system calls to some exe files.

My code has evolved a bit since then, but I think is a good example.

生生不灭 2024-08-10 05:41:58

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

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

You need to monitor the process exit-status separately, either by polling the exit code or by having a separate thread waiting in the Process.waitFor() method.

On the matter of buffering and flushing the streams, I don't thing there is an easy solution. There are several Java-classes which do buffering in different forms (BufferedInputStream, etc). Maybe one of them can help?

忆梦 2024-08-10 05:41:58

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

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

It's really important to consume the process standard output and error concurrently. See Carlos Tasada's example code elsewhere here.

If you don't do this your code may (or may not) work depending on the output from your spawned process. As and when that output changes (if your spawned process encounters an error, say) then without the concurrent consumption your process will deadlock. Most of the issues I see on SO related to Process.exec() are blocking related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文