Multiline paste in jshell

大兔子大兔子 提交于 2019-12-05 12:05:54

问题


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.


回答1:


So if you have code like this:

 int c = 2;
 int j = 4;
 int x = 5; 

Copy and paste into jshell, only the first two statements are processed.

But if you have code like this:

  int c = 2; int j = 4; int x = 5;

And paste into jshell:

jshell> int c = 2; int j = 4; int x = 5;
        c ==> 2
        j ==> 4
        x ==> 5 

Even more lines of code like this:

HashMap<Integer, Integer> map2 = new HashMap<>(); for (int i = 0; i < 15; ++i) { map2.put(i, i);map2.put(i, i); } System.out.println(map2);

will actually work.

Why? Me don't know.

The only way I know that copy/paste will work is via (type it in jshell) :

/edit

and you can paste as much as you want.




回答2:


I tried it and only the first two lines are processed. Also tried with extra newlines at the end and more than three lines, and still only the first two lines were ever processed. I don't know why but I suspect it's a bug.




回答3:


This was a bug. It has been fixed: https://bugs.openjdk.java.net/browse/JDK-8169595




回答4:


Just in case people still end up here, a small tweak to paste in an entire code block to jshell is to wrap it around with braces {} as:

{
 int c = 2;
 int j = 4;
 int x = 5; 
 // access these only in this scope though
 System.out.println("c : " + c + ", j : " + j + ", x = " + x);
}

Sample screen:



来源:https://stackoverflow.com/questions/41833901/multiline-paste-in-jshell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!