"jshell> /save" - Saving Code Snippet

This section provides a tutorial example on how to enter a long code snippet in 'jshell', save it to a file, and open it to run it later.

If you have entered a long code snippet, you use the "/save" command to save it to a file:

herong> jshell
|  Welcome to JShell -- Version 12.0.1
|  For an introduction type: /help intro

jshell> boolean is_prime;
is_prime ==> false

jshell> for ( int i=3; i<=30; i+=2 ) { 
   ...>    is_prime = true;
   ...>    for ( int j=2; j<=i/2; j++) { 
   ...>       is_prime = i%j > 0; 
   ...>       if (!is_prime) break;
   ...>    } 
   ...>    if (is_prime) {
   ...>       System.out.println("Found a prime number: " + i); 
   ...>    }
   ...> }
Found a prime number: 3
Found a prime number: 5
Found a prime number: 7
Found a prime number: 11
Found a prime number: 13
Found a prime number: 17
Found a prime number: 19
Found a prime number: 23
Found a prime number: 29

jshell> /list
   1 : boolean is_prime;
   2 : for ( int i=3; i<=30; i+=2 ) {
          is_prime = true;
          for ( int j=2; j<=i/2; j++) {
             is_prime = i%j > 0;
             if (!is_prime) break;
          }
          if (is_prime) {
             System.out.println("Found a prime number: " + i);
          }
       }

jshell> /save GetPrime.jsh

jshell> /exit
|  Goodbye (1)

The code snippet is saved in "GetPrime.jsh". You can load and run it with the "/open' command:

herong> jshell
|  Welcome to JShell -- Version 10.0.1
|  For an introduction type: /help intro

jshell> /open GetPrime.jsh
Found a prime number: 3
Found a prime number: 5
Found a prime number: 7
Found a prime number: 11
Found a prime number: 13
Found a prime number: 17
Found a prime number: 19
Found a prime number: 23
Found a prime number: 29

jshell>

Or you can run it directly from the command line:

herong> jshell GetPrime.jsh
Found a prime number: 3
Found a prime number: 5
Found a prime number: 7
Found a prime number: 11
Found a prime number: 13
Found a prime number: 17
Found a prime number: 19
Found a prime number: 23
Found a prime number: 29
|  Welcome to JShell -- Version 12.0.1
|  For an introduction type: /help intro

jshell>

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 java - The Java Program Launcher

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

jshell - Java Language Shell

 jshell - What Is It

 jshell - Entering Expressions and Statements

"jshell> /save" - Saving Code Snippet

 "jshell> /edit" - Editing Code Snippet

 jrunscript - Script Code Shell

 serialver - serialVersionUID Generator

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB