jshell

Sharing dynamically loaded classes with JShell instance

不问归期 提交于 2019-12-03 04:14:52
问题 Please view the edits below I'm trying to create a JShell instance that gives me access to, and lets me interact with objects in the JVM it was created in. This works fine with classes that have been available at compile time but fails for classes that are loaded dynamically . public class Main { public static final int A = 1; public static Main M; public static void main(String[] args) throws Exception { M = new Main(); ClassLoader cl = new URLClassLoader(new URL[]{new File("Example.jar")

How to pass arguments to a jshell script?

徘徊边缘 提交于 2019-12-03 01:59:30
Question I am willing to pass arguments to a jshell script. For instance, I would have liked something like this: jshell myscript.jsh "some text" and then to have the string "some text" available in some variable inside the script. However, jshell only expects a list of files, therefore the answer is: File 'some text' for 'jshell' is not found. Is there any way to properly pass arguments to a jshell script? Workaround so far My only solution so far is to use an environment variable when calling the script: ARG="some test" jshell myscript.jsh And then I can access it in the script with: System

How to shutdown jshell at the end of the script?

£可爱£侵袭症+ 提交于 2019-12-03 01:04:42
How to instruct jshell to terminate at the end of the script similarly to interpreters of other languages like for example python3 or node ? Following command ./jshell -q /tmp/shell.java with script /tmp/shell.java System.out.println("Hello world"); prints Hello world jshell> and waits for further commands. I'd like it to stop immediately at the end of the file. I'm looking for something more elegant than System.exit(0); at the end of the script. Jay Inside the script, use the jshell command /exit . This will exit jshell at the end of your script. Check this reference https://docs.oracle.com

Sharing dynamically loaded classes with JShell instance

限于喜欢 提交于 2019-12-02 17:34:14
Please view the edits below I'm trying to create a JShell instance that gives me access to, and lets me interact with objects in the JVM it was created in. This works fine with classes that have been available at compile time but fails for classes that are loaded dynamically . public class Main { public static final int A = 1; public static Main M; public static void main(String[] args) throws Exception { M = new Main(); ClassLoader cl = new URLClassLoader(new URL[]{new File("Example.jar").toURL()}, Main.class.getClassLoader()); Class<?> bc = cl.loadClass("com.example.test.Dynamic");//Works

How to run a JShell File?

删除回忆录丶 提交于 2019-12-01 15:52:01
I'd like to run an entire file with JShell like: $ jshell my-jshell-skript.java Where e.g. the content of my my-jshell-skript.java is 40 + 2; . Or alternatively an executable like: #!/usr/bin/jshell 40 + 2 Is this possible now or do I still have to take the old way over a Java-Main-Class? Edit 1: Windows-Problem On Windows, there is still no solution for me: C:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh 1 + 1 C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh | Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> /exit | Goodbye C:\JDKs\jdk9.0.0.0_x64\bin> JShell starts ignoring

Can I run jshell inside Unix expect?

久未见 提交于 2019-12-01 11:15:59
I'd like to redirect jshell input using expect, so that I can simulate typing in recorded demonstrations. But although I can spawn a jshell process from an expect script, which can also recognise the jshell prompt, after that nothing works. expect outputs what looks like a control sequence, like ^[[24;9R , and I don't see any output from jshell. Different terminal types produce different character sequences, but none of them work. This behaviour is consistent between expect on Ubuntu and Mac OS. Any suggestions for how to investigate this problem would be welcome. expect -d doesn't help. Here

Can I run jshell inside Unix expect?

限于喜欢 提交于 2019-12-01 09:18:27
问题 I'd like to redirect jshell input using expect, so that I can simulate typing in recorded demonstrations. But although I can spawn a jshell process from an expect script, which can also recognise the jshell prompt, after that nothing works. expect outputs what looks like a control sequence, like ^[[24;9R , and I don't see any output from jshell. Different terminal types produce different character sequences, but none of them work. This behaviour is consistent between expect on Ubuntu and Mac

How to run a java application with jshell?

谁说胖子不能爱 提交于 2019-12-01 09:11:39
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())); ...>

Different behaviour of same statement while executing on JShell

心不动则不痛 提交于 2019-11-30 23:00:03
I was working on a problem to store reference of two classes within each other For Example: class A { B b; A(B b){ this.b = b;} } class B { A a; B(A a){ this.a = a;} } public static void main(String...s){ A a = new A(new B(null)); a.b.a = a; } Now if instead of above initialisation, if I use below statement: A a = new A(new B(a)); I got below error which is quite obvious: Main.java:19: error: variable a might not have been initialised A a = new A(new B(a)); But if I try the same on JShell, it works just fine (Just to be extra sure that variable a has never been initialized, I checked for

What is the exact meaning / purpose of the J and R flags in jshell?

心不动则不痛 提交于 2019-11-30 20:40:35
From the help information: -J<flag> Pass <flag> directly to the runtime system. Use one -J for each runtime flag or flag argument -R<flag> Pass <flag> to the remote runtime system. Use one -R for each remote flag or flag argument I cannot find an explanation in both the tools documentation and jshell user guide . Also, what is "the remote runtime system" in the context of jshell? As I understand it, JShell has 3 main 'places' to execute code: In the current process (see DirectExecutionControl ) In the same JVM as the JShell client (see LocalExecutionControl ) On the remote agent (see