实施“制度” Java 中的命令
我需要一个“系统”函数调用,与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否是您正在寻找的,但您可以通过 JNA 库:
无论如何,粗略测试在 Windows 上是有效的。
Not sure if this is what you're looking for, but you can invoke the C
system
function through the JNA library:Cursory testing worked on Windows, anyhow.
如果我正确理解你,你想要这样的东西:
我在以下位置找到了这段代码: JavaWorld 不久前,当我正在寻找类似的解决方案来包装对某些 exe 文件的系统调用时。
从那时起,我的代码发生了一些变化,但我认为这是一个很好的例子。
If I'm understanding you properly, you want something like that:
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.
您需要单独监视进程退出状态,方法是轮询退出代码或让单独的线程在
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?同时使用流程标准输出和错误非常重要。。请参阅此处其他地方的 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.