Jul 14, 2017

OpenJDK 9: Jshell - how to load scripts and how to save/persist finished snippets

In my last posting i showed the builtin jshell commands and how to start working with the java shell.

What about loading and saving scripts?

I created a file myshell.txt with this content:
class MyClass {
 private int a;
 public MyClass(){a=0;}
 int getA() {return a;};
 void setA(int var) {a=var; return;}
}
MyClass ZZ;
ZZ = new MyClass();
ZZ.setA(200);
The help shows the following:
-> /help /open

|  /open

|  Open a file and read its contents as snippets and commands.

|  /open
|      Read the specified file as jshell input.
so i tried this one:
-> /open myjshell.txt
hmmm. No feedback inside jshell. But no news is good news:
-> /list

   1 : class MyClass {
        private int a;
        public MyClass(){a=0;}
        int getA() {return a;};
        void setA(int var) {a=var; return;}
       }
   2 : MyClass ZZ;
   3 : ZZ = new MyClass();
   4 : ZZ.setA(200);
 Saving your work is quite easy:
-> /help /save

|  /save

|  Save the specified snippets and/or commands to the specified file.

|  /save
|      Save the source of current active snippets to the file.

|  /save all
|      Save the source of all snippets to the file.
|      Includes source including overwritten, failed, and start-up code.

|  /save history
|      Save the sequential history of all commands and snippets entered since jshell was launched.

|  /save start
|      Save the default start-up definitions to the file.
And also no news is good news:
-> /save myjshell2.txt
and like expected:
$ cat myjshell2.txt
class MyClass {
 private int a;
 public MyClass(){a=0;}
 int getA() {return a;};
 void setA(int var) {a=var; return;}
}
MyClass ZZ;
ZZ = new MyClass();
ZZ.setA(200);
But what about this /save start ?
-> /save start myjshell3.txt
and the content of this file is:
$ cat myjshell3.txt

import java.util.*;
import java.io.*;
import java.math.*;
import java.net.*;
import java.util.concurrent.*;
import java.util.prefs.*;
import java.util.regex.*;
void printf(String format, Object... args) { System.out.printf(format, args); }
 To load a scipt on startup just type
jshell myjshell.txt



1 comment:

  1. Fascinating blog! Is your theme custom made or did you download it
    from somewhere? A theme like yours with a few simple tweeks would really make my
    blog jump out. Please let me know where you got your design. With thanks

    ReplyDelete