Java Linux Shell 申请
Java Linux Shell Application
所以我刚收到一个创建 Java Shell 应用程序的任务,没有使用任何第 3 方库,也没有使用 Runtime.exec() 或 ProcessBuilder API。
我不想要解决方案(显然我想自己做)但我确实需要提示如何做?我希望该应用程序打开一个 shell 提示符,它将接受使用 JDK 8(Nashorn?)的各种命令。
谢谢!
不是很清楚你想要实现什么。如果你想 运行 一个 Nashhorn shell 你可以这样实现 (Java 8)
import jdk.nashorn.tools.Shell;
public class NashornShell {
public static void main(String[] args) {
Shell.main(new String[]{ "-scripting"});
}
}
当您看到 Nashorn 提示时 jjs>
您可以执行 Linux 命令...
jjs> $EXEC("ls");
这将列出当前目录(使用 Linux ls
命令)。
...或执行Java命令...
jjs> java.lang.System.out.println("foo");
...或执行Java脚本命令...
jjs> print("foo");
有关详细信息,请查看 nashorn guide。
编辑 如果你只想传递 yourCommand
作为参数。
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"});
String yourCommand = "ls";
Object eval = engine.eval("$EXEC(\"" + yourCommand + "\")");
System.out.println(eval);
编辑 do you think that instead of Nashorn I could just use raw streams directed to the OS from JVM
以下是可能的
Commands.java
class Commands {
public static void main(String[] args) {
System.out.println("ls");
System.out.println("whoami");
}
}
run.sh
#!/bin/sh
java Commands | while read command; do
echo
echo "command: $command"
$command
done
但是当你想要 execute
Commands
的输出时显然不推荐这样做:
- 您的 Java 应用程序无法控制所执行的单个命令的 return 状态
- 如果一个命令等待用户输入,您的 Java 应用程序不知道它
- 您的 Java 应用程序无权访问命令产生的输出
- 所有命令都是盲目执行的
- 还有一些缺点
无论您尝试做什么,像 jdk.nashorn.tools.Shell 这样使用 nashorn 内部 class 都不是一个好主意。对于 java 9,此包不是 jdk.scripting.nashorn 模块的导出包。因此,使用 java 9,您将遇到包访问失败(即使没有安全管理器 - 因为模块 read/export 访问检查更像是 classes 的成员访问检查)。
所以我刚收到一个创建 Java Shell 应用程序的任务,没有使用任何第 3 方库,也没有使用 Runtime.exec() 或 ProcessBuilder API。
我不想要解决方案(显然我想自己做)但我确实需要提示如何做?我希望该应用程序打开一个 shell 提示符,它将接受使用 JDK 8(Nashorn?)的各种命令。
谢谢!
不是很清楚你想要实现什么。如果你想 运行 一个 Nashhorn shell 你可以这样实现 (Java 8)
import jdk.nashorn.tools.Shell;
public class NashornShell {
public static void main(String[] args) {
Shell.main(new String[]{ "-scripting"});
}
}
当您看到 Nashorn 提示时 jjs>
您可以执行 Linux 命令...
jjs> $EXEC("ls");
这将列出当前目录(使用 Linux ls
命令)。
...或执行Java命令...
jjs> java.lang.System.out.println("foo");
...或执行Java脚本命令...
jjs> print("foo");
有关详细信息,请查看 nashorn guide。
编辑 如果你只想传递 yourCommand
作为参数。
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"});
String yourCommand = "ls";
Object eval = engine.eval("$EXEC(\"" + yourCommand + "\")");
System.out.println(eval);
编辑 do you think that instead of Nashorn I could just use raw streams directed to the OS from JVM
以下是可能的
Commands.java
class Commands {
public static void main(String[] args) {
System.out.println("ls");
System.out.println("whoami");
}
}
run.sh
#!/bin/sh
java Commands | while read command; do
echo
echo "command: $command"
$command
done
但是当你想要 execute
Commands
的输出时显然不推荐这样做:
- 您的 Java 应用程序无法控制所执行的单个命令的 return 状态
- 如果一个命令等待用户输入,您的 Java 应用程序不知道它
- 您的 Java 应用程序无权访问命令产生的输出
- 所有命令都是盲目执行的
- 还有一些缺点
无论您尝试做什么,像 jdk.nashorn.tools.Shell 这样使用 nashorn 内部 class 都不是一个好主意。对于 java 9,此包不是 jdk.scripting.nashorn 模块的导出包。因此,使用 java 9,您将遇到包访问失败(即使没有安全管理器 - 因为模块 read/export 访问检查更像是 classes 的成员访问检查)。