Implementing the quot;systemquot; command in Java(实施“系统Java中的命令)
问题描述
我需要一个系统"函数调用,与 Python、Perl、PHP、Ruby 和 &c 中的函数调用相同.当它在 Rhino JavaScript 引擎上运行时,它将成为一个名为 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 的标准库似乎已经抽象出生成子进程的能力,该子进程共享父进程的标准输入输出.这意味着您不能将交互性推迟到子流程.
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.这使用三个泵"线程来主动独立地复制父进程的标准输入输出(以防止死锁).不幸的是,这给我们带来了两个问题.首先,当子进程自愿退出时,输入不会自动关闭.其次,子进程的流没有正确缓冲和刷新.
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.
我正在寻找能够使我们的 require("os").system() 命令按预期工作的解决方案.
I'm looking for solutions that would make our require("os").system() command work as one would expect.
该项目位于 http://narwhaljs.org
相关代码:
- 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
推荐答案
不确定这是否是你要找的,但你可以通过 system 函数"https://github.com/twall/jna/" rel="nofollow noreferrer">JNA 库:
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中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实施“系统"Java中的命令
基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
