JShell: Read-Evaluate-Print Loop for the Java Platform


참조 : http://jakubdziworski.github.io/java/2016/07/31/jshell-getting-started-examples.html

테스트 환경 : Windows 10, JDK 9 early access release

Running

C:\Program Files\Java\jdk-9\bin>jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

Default imports

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

Expressions

jshell> 3 + 3    // snippet
$1 ==> 6         // $1 == 스크래치 변수

jshell> $1 + 3
$2 ==> 9

Variables

jshell> int x=5
x ==> 5

jshell> x
x ==> 5

veryfing return type

jshell> 1/2

jshell> 1.0/2

jshell> 1.0f/2

Methods

jshell> void helloJShell() { System.out.println("hello JShell"); }
|  created method helloJShell()

jshell> helloJShell();
hello JShell

jshell> void helloJShell() { System.out.println("wow, I replaced a method"); }
|  modified method helloJShell()

jshell> helloJShell()
wow, I replaced a mthod
jshell> void helloJShell() { printMessage("Hello JShell"); }

jshell> void printMessage(String str) { System.out.println(str); }

jshell> helloJShell();

Hiding & Overloading

jshell> int x = 5;
x ==> 5

jshell> { int x = 3; System.out.println(x); } System.out.println(x);
3
5

jshell> void PrintX(int x) { System.out.println(x); }

jshell> PrintX(2); System.out.println(x);
2
5

Class

jshell> class Student {
   ...> private String name;
   ...> private String classRoom;
   ...> double grade;
   ...> public Student() { }
   ...> public String getName() { return name; }
   ...> public void setName(String name) { this.name = name; }
   ...> public String getClassRoom() { return classRoom; }
   ...> public void setClassRoom(String classRoom) { this.classRoom = classRoom; }
   ...> public double getGrade() { return grade; }
   ...> public void setGrade(double grade) { this.grade = grade; } }
|  created class Student

jshell> Student student = new Student();
student ==> Student@5702b3b1

jshell> student.<tab>
equals(       getClass()      getClassRoom()
getGrade()     getName()      grade
hashCode()     notify()       notifyAll()
setClassRoom(  setGrade(      setName(
toString()     wait(

Commands

snippet transformation

jshell> new JFrame<shift-tab>i
0: Do nothing
1: import: javax.swing.JFrame
Choice: 1
Imported: javax.swing.JFrame

jshell> new JFrame
jshell> new JFrame("Demo")<shift-tab>v

jshell> JFrame | = new JFrame("Demo")
jshell> JFrame frame = new JFrame("Demo")

jshell> frame.setSize(300, 200)
jshell> frame.setVisible(true)

jshell> frame.getGraphics()<shift-tab>v
0: Do nothing
1: Create variable
2: import: java.awt.Graphics. Create variable
Choice: 2
Imported: java.awt.Graphics

jshell> Graphics | = frame.getGraphics()

listing variables

jshell> /vars
|    int $1 = 6
|    int $2 = 9
|    int x = 5
|    Student student = Student@5702b3b1

listing methods

jshell> /methods
|    void helloJShell()

listing sources

jshell> /list
    1 : 3 + 3
    2 : $1 + 3
    3 : int x=5;
    4 : x
    6 : helloJShell()
    7 : void helloJShell() { System.out.println("wow, I replaced a method"); }
    8 : helloJShell()
    9 : class Student {
        private String name;
        private String classRoom;
        double grade;
        public Student() {}
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getClassRoom() { return classRoom; }
        public void setClassRoom(String classRoom) { this.classRoom = classRoom; }
        public double getGrade() { return grade; }
        public void setGrade(double grade) { this.grade = grade; } }
   10 : Student student = new Student();

history of what you have typed

jshell> /history

editing sources in external editor

jshell> /edit helloJShell

set editor

jshell> /set editor C:\Program Files (x86)\Microsoft VS Code\Code.exe

drop sources by name or id

jshell> /drop helloJShell
|  dropped method helloJShell()

list the declared types

jshell> /types x
|  This command does not accept the snippet 'x' : int x=5;

re-run last snippet

jshell> /!

save

jshell> /save name  -> name.jsh

open

jshell> /open name.jsh

exit the jshell

jshell> /exit

setting feedback modes

Mode Value Snippets Declaration Updates Commands Prompt
verbose name ==> value (and description) Yes Yes Yes \njshell>
normal name ==> value Yes No Yes \njshell>
concise name ==> value (only expressions) No No No jshell>
silent No No No No ->
jshell> /set feedback verbose 
|  Feedback mode: verbose

jshell> 2 + 2
$1 ==> 4
|  created scratch variable $1 : int

jshell> /set feedback silent 
-> 2 + 2
-> /set feedback normal 
|  Feedback mode: normal

jshell> 2 + 2
$3 ==> 4

jshell> /set feedback concise 
jshell> 2 + 2
$4 ==> 4
jshell>

snippet eval() status

DROPPED NONEXISTENT OVERWRITTEN RECOVERABLE_DEFINED
삭제 존재하지 않음 덮어씌움 복구 가능, 정의됨
RECOVERABLE_NOT_DEFINED REJECTED VALID -
복구 가능, 정의 되지 않음 실패 유효 -
import java.util.List;
import jdk.jshell.*;

public class Main {
    public static void showEvents(List<SnippetEvent> events) {
        for(SnippetEvent event : events)
            System.out.println(event);
    }

    public static void main(String... args) {
        JShell shell = JShell.create();

        showEvents(shell.eval("Integer x = 1, y = 2;"));
        showEvents(shell.eval("String getName() { return curName(); }"));
        showEvents(shell.drop(shell.methods().findFirst().get()));
        showEvents(shell.eval("Integer x = 5, y = z;" ));
    }
}


[type]        [target]                     [previous status]  [status]            
Snippet       (x)#1-Integer x = 1, y = 2;  NONEXISTENT        VALID               
Snippet       (y)#1-Integer x = 1, y = 2;  NONEXISTENT        VALID               
MethodSnippet getName()                    NONEXISTENT        RECOVERABLE_DEFINED
MethodSnippet getName()                    VALID              DROPPED            
Snippet       (x)#1-Integer x = 5, y = z;  VALID              VALID           
Snippet       (x)#1-Integer x = 1, y = 2;  VALID              OVERWRITTEN        
Snippet       (y)#2-Integer x = 5, y = z;  VALID              REJECT             
Snippet       (y)#2-Integer x = 1, y = 2;  VALID              OVERWRITTEN

Using External Library

참조 : https://kichwacoders.com/2017/05/25/woohoo-java-9-has-a-repl-getting-started-with-jshell-and-eclipse-january/

Eclipse January는 Java로 수치 데이터를 처리하기 위한 라이브러리입니다.
NumPy에서 영감을 얻었으며 비슷한 기능을 제공하는 것을 목표로 합니다.

January file download :

Run

"c:\Program Files\Java\jdk-9\bin\jshell.exe"  
--class-path org.eclipse.january_2.0.2.v201706051401.jar;
org.apache.commons.lang_2.6.0.v201404270220.jar;
org.apache.commons.math3_3.5.0.v20160301-1110.jar;
org.slf4j.api_1.7.10.v20170428-1633.jar;
org.slf4j.binding.nop_1.7.10.v20160301-1109.jar
import org.eclipse.january.dataset.*

Dataset dataset = DatasetFactory.createFromObject(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 })

dataset.toString(true)

dataset.resize(3, 3)

dataset.toString(true)

dataset.getShape()

dataset.getRank()

Dataset inplace = dataset.iadd(100)

inplace.toString(true)

Dataset slice = dataset.getSlice(new Slice(0, 2), new Slice(1, 3))

slice.toString(true)

Dataset dataset2 = DatasetFactory.createRange(15, Dataset.INT32).reshape(3, 5)

System.out.println(dataset2.toString(true))

J3D

C:\Program Files\Java\jdk-9\bin>jshell --class-path j3dcore.jar;j3dutils.jar;vecmath.jar
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> import com.sun.j3d.utils.geometry.*;

jshell> import com.sun.j3d.utils.universe.*;

jshell> import javax.media.j3d.*;

jshell> import javax.vecmath.*;

jshell> SimpleUniverse uni = new SimpleUniverse();

jshell> BranchGroup group = new BranchGroup();

jshell> Sphere ball = new Sphere(0.5f);

jshell> group.addChild(ball);

jshell> Color3f color = new Color3f(2, 2, 0);

jshell> BoundingSphere bounds = new BoundingSphere(new Point3d(0, 0, 0), 1000);

jshell> Vector3f direction = new Vector3f(5, -6, -10);

jshell> DirectionalLight light = new DirectionalLight(color, direction);

jshell> light.setInfluencingBounds(bounds);

jshell> group.addChild(light);

jshell> uni.getViewingPlatform().setNominalViewingTransform();

jshell> uni.addBranchGraph(group);

results matching ""

    No results matching ""