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
106
votes
3 answers

How to quit the JShell and go back to the command-line?

When using the JShell, how do I exit it back to the CMD line? I have already tried ctrl + x and just writing quit, but to no joy.
Bryn
  • 1,091
  • 2
  • 7
  • 8
69
votes
7 answers

How to import external libraries in jshell java 9?

I was trying to understand jshell and fumbled to import external library. As of date I couldn't see any suggestion/solution for this. Can someone please let me know if already figured this out.
Akshay
  • 1,231
  • 1
  • 19
  • 31
51
votes
4 answers

Java generics: why is this output possible?

I have this class: class MyClass { N n = (N) (new Integer(8)); } And I want to get these outputs: System.out.println(new MyClass().n); System.out.println(new MyClass().n.getClass()); Output of first…
Socke
  • 533
  • 4
  • 7
36
votes
3 answers

How to shutdown jshell at the end of the script?

How to instruct jshell to terminate at the end of the script similarly to interpreters of other languages like for example python3 or node? Following command ./jshell -q /tmp/shell.java with script /tmp/shell.java System.out.println("Hello…
czerny
  • 15,090
  • 14
  • 68
  • 96
34
votes
5 answers

In JShell, how to import classpath from a Maven project

I have a local Maven project under development. How can I launch jshell with the project class path with all the dependencies, so that I can test project or dependency classes inside JShell.
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
31
votes
4 answers

How to execute a java script with jshell?

Given that Java 9 is upon us and we can finally have a java REPL with jshell I was hoping there was a way to add a shebang to a script and have jshell interpret it. I tried creating test.jsh: #!/usr/bin/env jshell -s System.out.println("Hello…
steinybot
  • 5,491
  • 6
  • 37
  • 55
25
votes
3 answers

Sharing dynamically loaded classes with JShell instance

Please view the edits below I'm trying to create a JShell instance that gives me access to, and lets me interact with objects in the JVM it was created in. This works fine with classes that have been available at compile time but fails for classes…
Joba
  • 777
  • 7
  • 24
20
votes
7 answers

How to clear Java 9 JShell Console?

I didn't find any command to clear Java-9 JShell console. I also tried to clear the JShell Console through this program, but it doesn't work either. import java.io.IOException; class CLS { public static void main(String... arg) throws…
B. S. Rawat
  • 1,874
  • 3
  • 22
  • 34
19
votes
4 answers

How to pass arguments to a jshell script?

Question I am willing to pass arguments to a jshell script. For instance, I would have liked something like this: jshell myscript.jsh "some text" and then to have the string "some text" available in some variable inside the script. However, jshell…
Gwendal
  • 490
  • 3
  • 13
19
votes
5 answers

Multiline paste in jshell

I was trying out jshell and couldn't find option to paste multiple line expressions. Is it even possible to paste multiple lines in jshell. Similar to what scala offers with paste mode.
Kunal Kanojia
  • 253
  • 3
  • 9
17
votes
2 answers

What are the limits to JShell?

I found this question, and this other, so intriguing that it begs several questions, at least for me: Rather open-ended question, but where is jshell confined to? Obviously, GUI apps aren't in the domain for jshell solutions, or IDE…
Thufir
  • 8,216
  • 28
  • 125
  • 273
17
votes
1 answer

How to import a custom class in intellij JShell console

I am using the new intellij Jshell console (introduced here https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/) I created a simple class file Test2.java public class Test2 { public static String test(){ return "Hello"; …
Angel Koh
  • 12,479
  • 7
  • 64
  • 91
15
votes
1 answer

JShell error "unexpected type" when using specific class name

I was just playing around with JShell, and it seems that defining class Z{} and then defining var z = new Z() does not work. But using different class names, like class X and class A, does work. Surely I must be missing something obvious...? | …
user140547
  • 7,750
  • 3
  • 28
  • 80
15
votes
1 answer

Java 11 JShell inside Intellij IDEA

I have Java 11 JDK and IntelliJ IDEA 2018.2.4 (64-bit). When I was using Java 10.0.2, the JShell console in IntelliJ IDEA worked fine. Now that I've upgraded to Java 11, the JShell console has stopped working. Nothing at all happens when I click on…
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
15
votes
2 answers

Is there a way to use method references for top-level functions in jshell?

Suppose I do this in jshell: jshell> void printIsEven(int i) { ...> System.out.println(i % 2 == 0); ...> } | created method printIsEven(int) jshell> List l = Arrays.asList(7,5,4,8,5,9); l ==> [7, 5, 4, 8, 5, 9] jshell>…
adashrod
  • 456
  • 2
  • 12
1
2 3
13 14