Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use sysout without class and main method in Eclipse IDE using Java 9?

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 ?

like image 554
A_J Avatar asked Oct 18 '17 08:10

A_J


People also ask

How do I run a JShell in Eclipse?

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.

Where is a method used in eclipse?

Right click and select References > Project or References > Workspace from the pop-up menu. Show activity on this post. This will show you a Search view containing the hierarchy of class and method which using this method.


2 Answers

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.

like image 58
Christian Fries Avatar answered Sep 28 '22 19:09

Christian Fries


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.

like image 43
NilKha Avatar answered Sep 28 '22 20:09

NilKha