问题描述
我需要一个系统函数调用,与Python,Perl,PHP,Ruby和c中的调用相同。它将是一个名为Narwhal的JavaScript标准库的一个组件,当它在Rhino JavaScript引擎上运行时,该引擎又在Java上运行。
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.
麻烦的是Java的标准库似乎抽象出了生成共享父进程的stdio的子进程的能力。这意味着您无法将交互性推迟到子流程。
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.
我的第一个问题是实现Python的subprocess.popen。这使用三个pumper线程独立地主动复制父进程的stdio(以防止死锁)。不幸的是,这给了我们两个问题。首先,当子进程自愿退出时,输入不会自动关闭。其次,子进程的流不会缓冲和正确刷新。
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.
我正在寻找能够满足我们要求的解决方案(os)。system()按照预期的方式完成命令。
I'm looking for solutions that would make our require("os").system() command work as one would expect.
该项目位于
相关代码:
- http://github.com/tlrobinson/narwhal/blob/d147c160f11fdfb7f3c0763acf352b2b0e2713f7/lib/os.js#L10
- http://github.com/tlrobinson/narwhal/blob/d147c160f11fdfb7f3c0763acf352b2b0e2713f7/engines/rhino/lib/os-engine.js#L37
推荐答案
不确定这是不是你的意思寻找,但你可以通过:
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");
}
}
无论如何,在Windows上进行粗略测试。
Cursory testing worked on Windows, anyhow.
这篇关于实施“系统” Java中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!