19

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
Kunal Kanojia
  • 253
  • 3
  • 9

5 Answers5

14

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:

enter image description here

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Nice tip. Playing with `jshell` snippets I have found that it is more convenient (for me at least) to configure an _external editor_ (Notepad++ in my case) to easily enter multiple lines of snippets. The related details are described in the JShell documentation: [External Editor](https://docs.oracle.com/en/java/javase/12/jshell/editing.html#GUID-EA504020-D68D-4396-B67C-32C6736894BE); **NB!** To run the code entered using an external editor you must **close** your editor, and then the code will be executed. – informatik01 Apr 22 '19 at 14:50
12

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.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Thanks. I was just wondering if I was missing something or some undocumented command. – Kunal Kanojia Jan 25 '17 at 07:41
  • 1
    Just note that JSHELL respects editor preference environment variables like `JSHELLEDITOR`, `VISUAL`, and `EDITOR`. To use other editors, like Vim for example, `export JSHELLEDITOR=vim`. For all synopses of `/edit`, type `/help /edit` inside jshell. – Quar Jun 05 '19 at 21:55
  • Noting that at least as of now (mid-2020) pasting the first code example works perfectly fine. – Dave Newton Jul 31 '20 at 14:29
  • @DaveNewton ah! good to know. you might wanna edit the answer with that info if you want – Eugene Jul 31 '20 at 14:32
5

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.

Jason
  • 7,356
  • 4
  • 41
  • 48
4

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

Adobe
  • 12,967
  • 10
  • 85
  • 126
Robert Field
  • 479
  • 4
  • 5
0

If for some reason someone finds this because their situation forces them to use this version of the JDK with the bug in it, try this:

At the end of each line, add a \ character. As long as Jshell uses any of the usual encodings, such as ASCII or some variety of Unicode (and I would be surprised if it doesn't) and it allows escape characters (it wouldn't be much of a shell if it didn't) it will force the shell to process it as a single line.

The same is true for any shell, just as long as the conventions mentioned above are held, and in my experience, 99% of the time they are. The only anomaly that you may run into is a different character used as the escape character.

If your line doesn't end with a ; you will probably need to add a ;\ so that the line:

STATEMENT

is transformed to:

STATEMENT;\

Otherwise, if the ; is already present, just add the \.

The reason this works is because each time you enter a line such as:

COMMAND

depending on your OS, the shell sees:

COMMAND\n
# or...
COMMAND\r

The last bits are the newline (Mac and Linux) and carriage return (Windows) characters. These are special characters that tell the shell that the line breaks here. Since they are special characters, they can be escaped, or made to be interpreted as plain text, with the escape character \. This means that the shell sees:

COMMAND; \
COMMAND; \
COMMAND; \
COMMAND

...as:

COMMAND; COMMAND; COMMAND; COMMAND
```
Nate T
  • 727
  • 5
  • 21
  • To the triage reviewer: I realize that this is an old thread, but it felt appropriate to post this since the command line is under-taught to new programmers, even though, speaking for my self, it it the part of the interface where most of my time is spent during the development process. Anything that goes against that grain is a good idea in my eyes. – Nate T Feb 21 '22 at 23:32