score:7

Accepted answer

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

enter image description here

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:

enter image description here

score:1

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.

score:1

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:

quick shell terminal

you can also select your existing java source code and run it as a jshell script. for example :

quick shell execute as script

.jsh and .jpage files can be run from eclipse directly.

run jsh and jpage file

ps: i am author of this plugin.

score:4

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.

sample screenshot: enter image description here


Related Query

More Query from same tag