简体   繁体   English

Java Linux Shell应用程序

[英]Java Linux Shell Application

So I just received a task for creating a Java Shell App, without using any 3rd party libraries, and without using Runtime.exec() or ProcessBuilder APIs. 因此,我刚收到一个创建Java Shell App的任务,无需使用任何第三方库,也无需使用Runtime.exec()或ProcessBuilder API。

I don't want the solution (obviously I want to do this myself) but I do need a hint how to do this? 我不需要解决方案(显然我想自己做),但是我确实需要提示如何执行此操作? I want the app to open a shell prompt which will accept various commands with usage of JDK 8 (Nashorn?). 我希望该应用程序打开一个shell提示符,该提示符将接受使用JDK 8(Nashorn?)的各种命令。

Thanks! 谢谢!

Not really clear what you want to achieve. 不清楚要实现的目标。 If you want to run a Nashhorn shell you can achieve it like this (Java 8) 如果要运行Nashhorn Shell,则可以这样实现(Java 8)

import jdk.nashorn.tools.Shell;
public class NashornShell {
    public static void main(String[] args) {
        Shell.main(new String[]{ "-scripting"});
    }
}

When you see the Nashorn prompt jjs> you can execute Linux commands... 当您看到Nashorn提示符jjs> ,可以执行Linux命令...

jjs> $EXEC("ls");

which will list the current directory (using the Linux ls command). 它将列出当前目录(使用Linux ls命令)。

... or execute Java commands ... ...或执行Java命令...

jjs> java.lang.System.out.println("foo");

... or execute JavaScript commands ... ...或执行JavaScript命令...

jjs> print("foo");

For more information have a look in the nashorn guide . 有关更多信息,请参阅nashorn指南

edit If you want to pass only yourCommand as parameter. 编辑如果只想传递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);

edit do you think that instead of Nashorn I could just use raw streams directed to the OS from JVM 编辑 do you think that instead of Nashorn I could just use raw streams directed to the OS from JVM

Following is possible 以下是可能的

Commands.java 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

But obviously this is not to recommend when you want to execute the output of Commands : 但这显然不建议您在execute Commands的输出时使用:

  • your Java application has no control about the return state of the executed single commands 您的Java应用程序无法控制已执行的单个命令的返回状态
  • if one command wait for user input your Java application don't know it 如果一个命令等待用户输入,您的Java应用程序不知道
  • your Java application has no access to the output produced by the commands 您的Java应用程序无法访问命令产生的输出
  • all commands are blindly exected 所有命令都是盲目执行的
  • and some more downsides 还有更多的缺点

Regardless of what you're trying to do, using nashorn internal class like jdk.nashorn.tools.Shell is not a good idea. 无论您要做什么,使用nashorn内部类(如jdk.nashorn.tools.Shell)都不是一个好主意。 With java 9, this package is not an exported package of jdk.scripting.nashorn module. 在Java 9中,此软件包不是jdk.scripting.nashorn模块的导出软件包。 So, with java 9, you'll get package access failure (even in the absence of security manager - as module read/export access check is more like member access check for classes). 因此,使用Java 9,您将遇到程序包访问失败的情况(即使没有安全管理器,因为模块的读/导出访问检查更像是类的成员访问检查)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM