Questions tagged [jshell]

JShell is a Read–Eval–Print Loop (REPL) tool to evaluate declarations, statements, and expressions of the Java programming language, together with an API so that other applications can leverage this functionality.

The JShell provides a way to interactively evaluate declarations, statements, and expressions of the Java programming language within the JShell state.

The JShell state includes an evolving code and execution state. To facilitate rapid investigation and coding, statements and expressions need not occur within a method, expressions need not have side-effects, variables need not occur within a class, and methods need not occur within a class or interface.

Furthermore, JShell can be used to create and run scripts.

Goals

  • The JShell API and tool will provide a way to interactively evaluate declarations, statements, and expressions of the Java programming language within the JShell state. The JShell state includes an evolving code and execution state. To facilitate rapid investigation and coding, statements and expressions need not occur within a method, and variables and method need not occur within a class.

  • The JShell tool will be a command-line tool with features to ease interaction including: a history with editing, tab-completion, automatic addition of needed terminal semicolons, and configurable predefined imports and definitions.

Non-Goals

  • A new interactive language is not the goal: All accepted input must match grammar productions in the Java Language Specification (JLS). Further, within an appropriate surrounding context, all accepted input must be valid Java code (JShell will automatically provide that surrounding context -- the "wrapping"). That is, if X is an input that JShell accepts (as opposed to rejects with error) then there is an A and B such that AXB is a valid program in the Java programming language.

  • Out of scope are graphical interfaces and debugger support. The JShell API is intended to allow JShell functionality in IDEs and other tools, but the JShell tool is not intended to be an IDE.

201 questions
13
votes
7 answers

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…
user470370
  • 572
  • 1
  • 5
  • 17
13
votes
5 answers

Can the Java 9 jshell be used to run code in another JVM?

Java 9 has a read-eval-print loop for Java, called jshell. I've seen it work in it's basic mode, from the command line. Can it also be used in a remote process? In other words, can I connect to another Java process and enter code snippets to run…
Rob N
  • 15,024
  • 17
  • 92
  • 165
12
votes
2 answers

Debugging with JShell

I write a Java method in JShell and now I want to debug it. I would like to set breakpoints or at least step through an execution line-by-line. Does JShell have these debugging abilities?
ChaimKut
  • 2,759
  • 3
  • 38
  • 64
12
votes
2 answers

In jshell-11, why does a redeclared reference variable that resets to null still have a type?

When redeclaring Integer 'a' in line 33, why does jshell show the reference variable as an instance of Integer (refer to lines 38 & 39)? After the redeclaration, line 34 shows that 'a' is set to null. When 'a' is declared in line 6 but not given a…
B. Atkinson
  • 123
  • 5
12
votes
2 answers

JShell How to find the type of Variable or Result

How to find the type of a variable or Expression Result in JShell. Was experimenting Bitwise Operators in Java jshell> byte b=5<<-1; | Error: | incompatible types: possible lossy conversion from int to byte | byte b=5<<-1; | …
Sundar Rajan
  • 556
  • 4
  • 25
11
votes
1 answer

Jshell in intellij doesn't allow generic types

I just started using jshell in intellij idea community version. When I write the below code in jshell, it works. List list = List.of("a", "b", "c"); System.out.println(list); However the same code doesn't work in intellij. It says…
Need Help
  • 113
  • 5
10
votes
1 answer

In Java, what does the / (i.e., forward slash) mean in object references like $Lambda$15/0x00000008000a9440@32e6e9c3)?

In JShell, if I do this: interface Foo { String foo(); } (Foo) () -> "hi" I get | created interface Foo $2 ==> $Lambda$15/0x00000008000a9440@32e6e9c3 From the research below, I know the following: $Lambda = an in-memory reference, as opposed to…
mellow-yellow
  • 1,670
  • 1
  • 18
  • 38
10
votes
1 answer

Why and how do you use JShell?

I went through a couple of tutorials for JShell and I still do not understand the most important part: why would I even use JShell? Here what Oracle says: "You can test individual statements, try out different variations of a method, and…
displayname
  • 1,153
  • 1
  • 9
  • 21
10
votes
4 answers

Is it possible to use sysout without class and main method in Eclipse IDE using Java 9?

As Java 9 introduced the concept of JShell which enables us to write code without creating a class and a method, is it possible to use this feature of Java 9 in eclipse ?
A_J
  • 977
  • 4
  • 16
  • 44
10
votes
1 answer

JShell won't execute anything using Git Bash

I installed Java9 (currently available version on Oracle's site) for Windows (Windows 10 x64 Professional, up to date as well, runs as a vmware virtual machine->VMware Workstation 12). I added the jdk\bin to the User's Path and tried to use Git Bash…
szab.kel
  • 2,356
  • 5
  • 40
  • 74
10
votes
2 answers

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

From the help information: -J Pass directly to the runtime system. Use one -J for each runtime flag or flag argument -R Pass to the remote runtime system. …
rmuller
  • 12,062
  • 4
  • 64
  • 92
10
votes
3 answers

Java 9 REPL for running app

Java 9 is introducing REPL called JShell inside the JDK distribution. Is there any way to connect into the JShell of the JDK that is running some application and execute commands referencing that running app. For example executing some methods form…
Saša Šijak
  • 8,717
  • 5
  • 47
  • 82
9
votes
1 answer

JShell access to variables defined outside of jshell instance

From inside jShell script, is it possible to access or register variables that are defined in code that's also creating JShell? Currently there seems to be no mechanism to either access or register a variable to Shell instance, or return none string…
Developer
  • 125
  • 6
8
votes
1 answer

Create Module in JShell in Java 9

Just exploring new release of Java, its new module system, and playing with jshell as well. Probably my question doesn't have too much sense, but I am just curious. So I came up with the question: Is there any way to create a module in jshell? Or…
Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
1
2
3
13 14