实施“系统"Java中的命令

2024-08-24Java开发问题
1

本文介绍了实施“系统"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中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13