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
1 answer

View or print a JavaDoc in Jupyter Notebook?

I'm using Jupyter Notebook with the IJava Java kernel via for learning and testing code. IJava utilises jshell. Is there a way to print the JavaDoc for a specific class or function? I can print Python docstrings in a Jupyter notebook in several…
Mark Teese
  • 651
  • 5
  • 16
2
votes
2 answers

How to set the Java version in JShell?

I'm using JShell from JDK 11 to experiment some behaviors with generics. I would like to set the interpreted java version to version 8. This would allow me to have the same type inference behaviors as defined in the Java Language Specification…
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
2
votes
1 answer

JShell Edit Pad running only one line when accepting

I'm learning to use jshell, and tried to write a snippet of code in the edit pad. When hitting accept, if all the code is in the window only the first line is processed and saved. If I close and open the edit pad, all other lines vanish. I can use…
Alessandro
  • 21
  • 2
2
votes
1 answer

JShell: Accessing Objects Created by Snippets

I am very confused about something and I would appreciate some insight here. Say I want to build a GUI that visualizes what is going on inside JShell, i.e. how the, by snippets created objects reference each other and what, by Snippets created…
Raphael S
  • 41
  • 3
2
votes
1 answer

jshell tab completion non-instance methods

The new Java shell, jshell, allows tab completion which shows all methods available to a given instance of a class. For example, If I do... jshell> Integer myInt = 3 myInt ==> 3 jshell> myInt. <<< + TAB >>> byteValue() compareTo( …
awwsmm
  • 1,353
  • 1
  • 18
  • 28
2
votes
1 answer

Can I run jshell inside Unix expect?

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…
Maurice Naftalin
  • 10,354
  • 2
  • 24
  • 15
2
votes
1 answer

Different ways to load script on jshell startup

As per the documentation, There’s also the option to load a script on startup, including some special predefined options. These are specified using the —startup flag, passing in either a filename or one of: DEFAULT – load the default behavior. This…
KayV
  • 12,987
  • 11
  • 98
  • 148
2
votes
1 answer

JShell short-cut for going at the start of line and going at the end of line

is there any short-cut available for moving the cursor at the start of a line and at the end of the line? Rather than moving arrow left and right can we use any shortcut for the same?
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
2
votes
1 answer

How to import another script file in Jshell

I'm looking to use jshell to replace bash for command line processing. I've created a simple class fs in the file fs.jsh (yes poor naming) that has a number of utility functions like: // file fs.jsh class fs { static void println(String line) …
Brett Sutton
  • 3,900
  • 2
  • 28
  • 53
2
votes
1 answer

Is there a way to reset default imports of JShell?

Following the post Is there a way to remove imports in JShell?. I was wondering if there was a way to get rid of the default imports as well. jshell> /imports | import java.io.* | import java.math.* | import java.net.* | import…
Naman
  • 27,789
  • 26
  • 218
  • 353
2
votes
0 answers

JShell code performance

As far as I understand JShell statements are wrapped in synthetic classes when the code snippet is evaluated the first time. This is documented in the JEP-222. Can I conclude that excluding the first evaluation of the snippet (let's say a method…
greg
  • 306
  • 1
  • 13
2
votes
1 answer

Statement oriented approach against expression oriented one

I was going through this article on java9 and came across this line where it states that Java is statement-oriented whereas REPLs are expression-oriented. Can somebody explain the difference between these two?
Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22
2
votes
2 answers

What is going on with equality in JShell/Java 9?

I've been using JShell a bit just to test it out, and today I came across quite the interesting bit of behavior. jshell> String a = "A" a ==> "A" jshell> String b = "A" b ==> "A" jshell> a == b $4 ==> true jshell> "A" == "A" $5 ==> true I was…
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
2
votes
1 answer

Killing an infinite loop freezes the terminal

There is a similar question already posted on SO, however, the solution works only if the loop doesn't generate any output. If instead I try to run while(true) System.out.print(1); and hit the Ctrl+C afterwards, what happens is the terminal just…
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
1
vote
0 answers

Add local variable to JShell evaluation context

I'm using jdk.jshell.JShell to evaluate Java code programmatically and I'd like to add a local variable to the active evaluation context, so the value is useable in the shell. I'm instantiating the JShell with a DirectExecutionControl, so it runs on…
zdimension
  • 977
  • 1
  • 12
  • 33