How to execute a java script with jshell?

Use //usr/bin/env jshell –show-version –execution local “$0” “$@”; exit $? as the first line of test.jsh. The test.jsh script could look like: //usr/bin/env jshell –show-version “$0” “$@”; exit $? System.out.println(“Hello World”) /exit The command line option –show-version is optional, of course, but gives immediate feedback that the tool is running. The extra command line option … Read more

In JShell, how to import classpath from a Maven project

You can use the jshell-maven-plugin: mvn com.github.johnpoth:jshell-maven-plugin:1.3:run which will fire up a JShell session with your project’s runtime path. If you want to include your test dependencies just add -DtestClasspath to the command. NOTE: the plugin expects the project to be built already. If not, invoke the appropriate Maven build phase before the plugin e.g: … Read more

Java generics: why is this output possible?

The behavior that IntelliJ shows is clear to me: You have an unchecked cast in MyClass. This means new Integer(8) is not immediately cast to Long but to the erasure Number (which works), when this line is executed: N n =(N)(new Integer(8)); Now let’s look at the output statements: System.out.println(new MyClass<Long>().n); boils down to String.valueOf(new … Read more

How to import external libraries in jshell java 9?

I tried with 9 Build 162 Linux 64-bit with preparation: Downloaded guava-19.0.jar and commons-lang3-3.4.jar to /opt/libs The following options are available: Specify CLASSPATH environment variable: $> CLASSPATH=”/opt/libs/commons-lang3-3.4.jar:/opt/libs/guava-19.0.jar” bin/jshell Specify classpath with jshell option: $> bin/jshell –class-path /opt/libs/guava-19.0.jar:/opt/libs/commons-lang3-3.4.jar Configure evaluation context within jshell session with command /env, /reset or /reload(these commands are different, you can check … Read more