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 ?
-
you can wait for Eclipse support for JShell. But if you want to do it without JShell- then you cannot – mlecz Oct 18 '17 at 08:21
-
1Why has been my question downvoted ? Please let me know so that I can improve my question in future – A_J Oct 18 '17 at 08:25
-
it´s a very generic/basic/.. question and Stackoverflow is for solving code-issues with some example code. – LenglBoy Oct 18 '17 at 08:35
-
And here is a [how-to-jshell-eclipse](https://kichwacoders.com/2017/05/25/woohoo-java-9-has-a-repl-getting-started-with-jshell-and-eclipse-january/) for you. – LenglBoy Oct 18 '17 at 08:36
-
5There is Eclipse’s [Scrapbook](https://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/concepts/cscrapbook.htm) feature for eons now. I really don’t understand the big fuzz about JShell… – Holger Oct 18 '17 at 08:43
-
@Holger, with a well-integrated jshell in some IDE of your choice you can easily use and script your own custom classes and libraries. I guess that is appealing to those praising jshell. I. e. with Intellij it is just: Add target folder of your project to libraries once, run jshell, put in some code, hit one key comb et voilá. – daniel.kahlenberg Sep 24 '19 at 07:16
4 Answers
You can use the TM Terminal to run JShell in Eclipse:
- If necessary, install TM Terminal (contained only in some Eclipse packages)
- Open a 'Terminal' view in Eclipse: Window > Show View > Other...: Terminal > Terminal
- Launch a new Local Terminal
- Run JShell, e. g. on Windows type
"C:\Program Files\Java\jdk-9\bin\jshell" -v
followed by Enter
Alternatively, you can use a Scrapbook Page, a built-in feature of the Eclipse Java IDE and which also works with older Java versions. You will have code completion and you can use Java classes of your project:
-
You should probably format your answer better, as it's hard to read it. Thanks. – Dawid Zbiński Nov 23 '17 at 18:50
-
-
I managed to run the jshell from the terminal, but I dont want to do this all the time. How can I create a menu to make all those steps automatically and save me time? – user5193682 Sep 11 '18 at 08:45
If this is not a feature ask for Eclipse, a very basic stub that you can come up with is:
public static void main(String[] args) throws Exception {
jdk.jshell.tool.JavaShellToolBuilder.builder().run();
}
When you execute this, you can further use your debug console as JShell in your IDE.

- 27,789
- 26
- 218
- 353
-
When I write System.out.print("Hello"); and press Enter, the cursor keeps on blinking in the next line, it doesn't give any output. If I do the same thing in cmd prompt I get the output "Hello" in the next line. Why is the behavior different and how to execute jshell code in eclipse ? – A_J Oct 19 '17 at 02:41
-
1No output printing for me in Eclipse. The cursor keeps on blinking in the next line. I am using latest Oxygen version. I suppose you are using a different editor. – A_J Oct 19 '17 at 22:38
If you like to use JShell (from Eclipse or from a Terminal) to try out code, a very nice option is to use the Maven JShell plugin and just run mvn from a corresponding (dummy) project (in Eclipse: Right-Click on the Project and Run As -> Maven build).
In that case JShell knows all the libraries specified in the dependencies of the project.
I am use this here: http://www.finmath.net/finmath-experiments/montecarlo-blackscholes/
A small pom.xml using some libraries (JavaFX, Apache commons, finmath lib) could look like this:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.finmath</groupId>
<artifactId>finmath-experiments</artifactId>
<version>0.1.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean install jshell:run</defaultGoal>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- Java FX -->
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
<!-- finmath-lib -->
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib-plot-extensions</artifactId>
<version>0.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
</dependencies>
</project>
Note: Personally I prefer running this from a Terminal in macOS, since JShell supports "TAB-auto-completion" there, which appears to be missing when running from Eclipse.

- 16,175
- 10
- 56
- 67
There are multiple ways to do this as explained in other answers. But I would like to tell you a plugin which will provide more feature than just starting a normal JShell from Eclipse.
Check this Eclipse plugin QuickShell
This plugin will start JShell in Eclipse terminal. Like this:
You can also select your existing java source code and run it as a JShell script. For example :
.jsh and .jpage files can be run from Eclipse directly.
PS: I am author of this plugin.

- 104
- 3
- 10