Multiline paste in jshell

若如初见. 提交于 2019-12-03 23:29:39

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.

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.

Robert Field

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

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:

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