如何使用jshell运行java应用程序?它应该能够指定类路径并调用java命令并传递一些参数,如bash do,例如,
#!/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)
解决方法:
对于在jshell中运行任何应用程序,首先在开始时将类路径设置为jshell.
例:
jshell -class-path /Users/sree/Desktop/libs/jettison-1.0.1.jar
然后将类导入到运行的环境中
import org.codehaus.jettison.json.JSONObject;
这会将所需的类导入到环境中.
现在运行所需的应用程序.就我而言,我进入了
String myString = new JSONObject().put("JSON", "Hello, World!").toString()
并得到了输出
myString ==> "{\"JSON\":\"Hello, World!\"}"
回答传递命令行参数的问题.你必须用所有的值来启动课程.
Test instance = new Test("data","data1")