如何使用 jshell 运行 java 应用程序?它应该能够指定类路径并调用 java 命令并像 bash 一样传递一些参数,例如,
#!/bin/bash
$ARGS=...
$CLASSPATH=...
java -cp $CLASSPATH $ARGS com.example.MyApp
更新:
我认为需要运行时或进程的包装器,例如,
jshell> private String executeCommand(String command) {
...>
...> StringBuffer output = new StringBuffer();
...>
...> Process p;
...> try {
...> p = Runtime.getRuntime().exec(command);
...> p.waitFor();
...> BufferedReader reader =
...> new BufferedReader(new InputStreamReader(p.getInputStream()));
...>
...> String line = "";
...> while ((line = reader.readLine())!= null) {
...> output.append(line + "\n");
...> }
...>
...> } catch (Exception e) {
...> e.printStackTrace();
...> }
...>
...> return output.toString();
...>
...> }
| Created method executeCommand(String)
jshell> String out =executeCommand("java --version");
out ==> "java 9.0.4\nJava(TM) SE Runtime Environment (bui ... d 9.0.4+11, mixed mode)\n"
jshell> System.out.println(out);
java 9.0.4
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)