How to run a java application with jshell?

本小妞迷上赌 提交于 2019-12-19 09:42:08

问题


How to run a java application with jshell? It should be able to specify classpath and call java command and pass some arguments like bash does,e.g,

#!/bin/bash
$ARGS=...
$CLASSPATH=...
java -cp $CLASSPATH $ARGS com.example.MyApp

Update:
I think a wrapper of Runtime or Process is required, e.g.,

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)

回答1:


For Running any application in jshell first set the classpath to the jshell at the starting.

Example:

jshell -class-path /Users/sree/Desktop/libs/jettison-1.0.1.jar 

Then import the class into the environment running

import org.codehaus.jettison.json.JSONObject;

This will import the required class into the environment.

Now run the required application. In my case, I entered

String myString = new JSONObject().put("JSON", "Hello, World!").toString()

And got back the output

myString ==> "{\"JSON\":\"Hello, World!\"}"

answering the question for passing command line arguments. you have to initiate the class with all the values.

Test instance = new Test("data","data1")



回答2:


Your code is broken, as you are waiting for the command’s completion first and only afterwards reading the pipe. If the command produces more output than the pipe can buffer, the command will get blocked and never complete, as no-one is reading the pipe at this point.

In a perfect world, you would just use

new ProcessBuilder(your command and args).inheritIO().start().waitFor()

as in a normal Java application. Unfortunately, jshell changes the standard file descriptors in a way that this doesn't work (at least in the Windows version I tested).

You can use

void run(String... arg) throws IOException, InterruptedException {
  Path tmp = Files.createTempFile("run", ".tmp");
  try {
    new ProcessBuilder(arg).redirectErrorStream(true).redirectOutput(tmp.toFile())
      .start().waitFor();
    Files.lines(tmp, java.nio.charset.Charset.defaultCharset())
      .forEach(System.out::println);
  }
  finally { Files.delete(tmp); }
}

and call it like, e.g.

run("java", "-version")

or

run("java", "-d", "java.base")


来源:https://stackoverflow.com/questions/49072972/how-to-run-a-java-application-with-jshell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!