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
2
votes
2 answers

How to use Lombok in jshell?

How I can use Lombok annotation in JShell? I tried * jshell --class-path lombok.jar jshell> import lombok.* jshell> @Data class Person { String name; String address; } | created class Person jshell> new Person() $3 ==> Person@6f7fd0e6 jshell>…
andreoss
  • 1,570
  • 1
  • 10
  • 25
2
votes
1 answer

Why does jshell show this number?

I am learning java and this logic makes me feel confused. Isn't here i=20(+1)+20(+1)? Why 41 instead of 42? jshell> int i = 20 i ==> 20 jshell> i=i++ + i++ i ==> 41 See this code run at Ideone.com.
Jack
  • 23
  • 3
2
votes
1 answer

What is Java module jdk.editpad?

The JDK ships with a module jdk.editpad that exports no services or packages. The Javadoc describes this module as: Provides the implementation of the edit pad service used by jdk.jshell. But if you look at the modules depended on by jdk.jshell,…
Archie
  • 4,959
  • 1
  • 30
  • 36
2
votes
0 answers

Why does JShell have an echo in my shell demo?

I am writing a simple shell demo that highlights a problem I am having with JShell. When I pass JShell as an argument to /bin/zsh -c it works fine except for the fact that my Process echoes back whatever I send it. Here is the code to my shell…
ktm5124
  • 11,861
  • 21
  • 74
  • 119
2
votes
0 answers

Java Jshell keyboard macros

I have been reading the Oracle Jshell User Guide, one feature of Jshell Hotkeys escapes my grasp, the so called keyboard macro function, by definition it sounds quite useful, you should be able to press CTRL+x ( then type a Word hit CTRL+x ) and if…
DHC19
  • 109
  • 1
  • 6
2
votes
1 answer

Why jshell allows non-final free variables in lambda expressions?

In a .java file, following will not compile: class Test { public static void main(String[] args) { int x = 0; Runnable r = () -> System.out.println(x); r.run(); x++; r.run(); } } However in jshell,…
2
votes
2 answers

Unexpected Type Error when using Unary Operator --

I am learning Java and am experimenting with the unary operators --expr, expr--, and -expr. In class, I was told that --3 should evaluate to 3. I wanted to test this concept in the following assignments: jshell> int t = 10; t ==> 10 | created…
Tech320
  • 39
  • 5
2
votes
1 answer

Cursor movement keys not working properly in jshell on macOS terminal

In the following start to a snippet I had typed some nonsense asd that I then hit backspace to overwrite: jshell> void asd$<3$<3> As can be seen instead of backspace I got $<3 . Right Arrow similarly does $<2 . Actually Left Arrow works properly. …
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
2
votes
0 answers

Getting JShell UnresolvedReferenceException when trying to use methods from external library in the image created with JLink tool

I created an application using the JShell API and I'm loading some external libraries using the method addToClasspathand I'm using JavaFX for the GUI, additionally I'm loading some snippets from external files. This is working fine when using the…
LuisOsv
  • 339
  • 1
  • 4
  • 13
2
votes
1 answer

Question about return type( need to return instances of different classes) in generics

So my current code looks like this. The idea is to take in an instance of different classes using generic type T and return those instances. I should be able to call instances of classes like this new A().add(new B()) public static T
L m
  • 551
  • 1
  • 5
  • 8
2
votes
1 answer

How to run jshell when I don't have a file to execute?

I would like to run jshell like this $ jshell --execute 'System.out.println("Hello World")' Hello World $ So it executes the java single code line and terminates. Is it possible?
ieXcept
  • 1,088
  • 18
  • 37
2
votes
1 answer

Java Runtime version for JDK12

It's not really an error but when I am checking the runtime version it gives me the following, does that mean am I doing something wrong? jshell> Runtime.version() $3 ==> 12.0.1+12
Venkateshreddy Pala
  • 873
  • 1
  • 6
  • 11
2
votes
2 answers

How to define an empty list of string in jshell in a shortest possible way?

I want to create an empty list of type List in jshell I am hoping jshell supports some sort of syntax shorthand for example: List args = [] However it failed with this error message: | Error: | illegal start of expression | …
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
2
votes
1 answer

Java 12 Shell not reading keyboard input properly on Windows

Has the Java Shell changed the way it reads keyboard input in Java 12? It sometimes misses keystrokes, and the arrow keys often print random letters instead of bringing up the previous line. This is happening in Java 12 using Oracle's OpenJDK on…
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
2
votes
0 answers

Using final variable in JShell

I was trying to learn with JShell in JDK11, jshell> final Integer val = 1 But I get a warning as : | Warning: | Modifier 'final' not permitted in top-level declarations, ignored Can I not use final in JShell or am I doing something wrong?
user10824969