Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
"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
javac - The Java Program Compiler
java - The Java Program Launcher
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jshell - Entering Expressions and Statements
►"jshell> /save" - Saving Code Snippet
"jshell> /edit" - Editing Code Snippet
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter