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

How to programatically add variables to the Shell from which some code was invoked?

with jshell, is there a way to create variables in the scope of the interpreter? I have something like Map vars = loadVarsFromSomething(); and I would like to be able to do something like for ( Map.Entry key : vars…
crow
  • 1
  • 1
  • 10
4
votes
1 answer

Can the jshell command `/open` work relatively to the folder containing the script, instead of the working directory?

I have two jshell scripts script.jsh and commons.jsh in the same folder /path/folder. script.jsh: /open commons.jsh commons.jsh: System.out.println("commons.jsh loaded") If I am in /path/folder, I can run jshell script.jsh and it works well If I…
Gwendal
  • 490
  • 3
  • 13
4
votes
2 answers

What is the use of C option in jshell

I have gone through the introduction to jshell guide and couldn't find description/examples about the -C option in jshell. $jshell --help -C Pass to the compiler. Use one -C for each compiler flag…
Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
4
votes
1 answer

Is there an alternate way to launch remote JVM?

I am playing with the JDK9's JShell API. At present, the JShell API automatically launches the remote JVM internally. Instead of starting the process automatically, I want to separate the two process. Note: I understand I can change the VM options.…
JackDaniels
  • 985
  • 9
  • 26
4
votes
2 answers

jshell continues executing my script after exception is thrown. How to make it stop?

I tried writing some jshell scripts. When an exception is thrown, jshell still goes on to execute the next line. How can I make my scripts behave more like normal java programs? Edit: I simply run it like jshell SCR.jsh. Using throw new Exception()…
cshu
  • 5,654
  • 28
  • 44
4
votes
1 answer

NoClassDefFoundError while trying to use jdk.incubator.http.HttpClient in java in Eclipse Oxygen

Here is the code snippet that I use: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder(URI.create("https://www.google.com")).GET().build(); HttpResponse.BodyHandler responseBodyHandler =…
Kavitha Karunakaran
  • 1,340
  • 1
  • 17
  • 32
4
votes
0 answers

Using non-default editors with /edit command

I was recently trying to use non-default editors (such as atom or sublime) with the /edit command in JShell. To do that, I set the editor with the following command: /set editor -wait atom As the documentation states, when I type the /edit command…
gajos
  • 897
  • 2
  • 10
  • 21
4
votes
2 answers

Looking for JShell input workaround

I'm trying out the new JShell program with the java9 jdk. I want to debug an interactive console app I'm making, but it seems System.in doesn't work. For example: [root@mycomputer home]# jdk-9/bin/jshell | Welcome to JShell -- Version 9-ea | Type…
flakes
  • 21,558
  • 8
  • 41
  • 88
4
votes
1 answer

Convert variable to its original form in JShell

I'm playing with the JShell and trying to convert a variable to its original form. To do that, I reset everything in the REPL using /r, then I imported java.util.* and created a list: -> import java.util.*; -> List l = new…
Maroun
  • 94,125
  • 30
  • 188
  • 241
3
votes
0 answers

How to set lineeditor to vi-mode in JShell

How to set lineeditor to vi-mode in JShell? The setting set editing-mode vi is in both $HOME/.inputrc and $HOME/.jline.rc, but it seems like Jshell does not respect any of them.
FooBee
  • 778
  • 7
  • 23
3
votes
1 answer

What other than java.lang packages are imported automatically in the JShell?

I've observed that in the JShell session, not only package "java.lang", but quite a few other packages (that are not imported automatically in the Java class files, e.g. LinkedList, Math and several other types) seem to be imported, by default. I…
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
3
votes
1 answer

In JShell, How to evaluate whole java code?

I'm using the JShell API to run Java code. However, I got an error when I run a whole code. For example: import jdk.jshell.JShell; var code= """ void hello(){ System.out.println("Hello world"); } …
Rahman Usta
  • 716
  • 3
  • 14
  • 28
3
votes
0 answers

JShell no longer prints output on next line after JDK update

Before updating JDK, any output in JShell would be automatically printed onto the next line. However, after updating, all the outputs are immediately printed onto the same line. This makes it completely unreadable and borderline unusable when…
Type Definition
  • 75
  • 1
  • 13
3
votes
1 answer

Run echo example from Jshell

I read such a command line arguments example: public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } } It runs properly from command line, how could I run…
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
3
votes
1 answer

How to define a custom prompt in JShell from a script?

I am trying to create a custom JShell prompt and want to load it from a file directly. But it is not showing the custom prompt when loading the set prompt from a file, but I am able to do it writing the same commands on JShell. Ex: I have a file…