Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a JShell File?

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 my file completely. Is it a bug?

Edit 2: Solution for Windows-Problem

Turns out that it is the content of my foo. Seems like 1 + 1 does only work "on the fly", not read from a file:

C:\JDKs\jdk9.0.0.0_x64\bin>type foo.jsh
System.out.println("foo");

C:\JDKs\jdk9.0.0.0_x64\bin>jshell.exe foo.jsh
foo
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> /exit
|  Goodbye

C:\JDKs\jdk9.0.0.0_x64\bin>
like image 886
user470370 Avatar asked Sep 26 '17 12:09

user470370


People also ask

How do I run a JShell in terminal?

To start Jshell, first we must have installed Java 9 then open terminal in Linux or command prompt in windows and type jshell. It will start jshell session and displays a welcome message to the console. To display a simple “Hello Java” message, write print command without creating class and hit enter.

What is JShell command?

The JShell tool is a command line tool that facilitates exploratory programming by providing interactive use of Java Programming Language elements. JShell is a REPL (Read-Evaluate-Print-Loop). It is ideal both for learning the Java language and exploring unfamiliar code (include new Java APIs).

Is JShell an interpreter?

What is JShell? One of the new features in Java 9 is JShell which is a Read-Eval-Print-Loop (REPL) interpreter. A REPL is an interactive shell that allows execution of single statements without the need to compile a complete program.


2 Answers

Launch jshell in concise feedback mode and filter the required content-

$echo '40 + 2' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\1/gp'

output: 42

More details here- How to execute java jshell command as inline from shell or windows commandLine

like image 177
Rahul Sharma Avatar answered Sep 21 '22 07:09

Rahul Sharma


JShell is not meant to run a Java class directly. If you want to run a java class, you still need to do it the old way - java <your-class-name>.

From the docs,

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.

As per this quote, JShell is meant for running or trying out individual Java statements. In the traditional java way, you have to write a full Java program before you can run it and see the results. But JShell allows you a way to try out the Java statements without needing you to build the full standalone java application.

So the short answer to your question is that, no, you can't call standalone java applications like jshell my-jshell-skript.java. However, you CAN call a script file which contains individual JShell commands or Java statements. So if you copy all the statements from your Java program and paste them to a JShell script, you can run the script like:

% jshell my-jshell-skript.jsh

But this is not quite the same as running a standalone java application.

like image 34
VHS Avatar answered Sep 21 '22 07:09

VHS