-
Notifications
You must be signed in to change notification settings - Fork 0
Hello, World!! JavaShell Introduction Part 2
##This guide is written on Release 0.1.x
#This is a Hello, World guide on JavaShell. Part 2.
If you haven't done it yet, please follow Part 1 first.
You will be creating a Hello World program in this part of the tutorial.
Load the javashell if you haven't already.
java JavaShell
##Goal We will be writing the code that mimics the behaviour of the following java code.
System.out.println("Hello,_World!!!");##1. Construct a Hello,_World String
To construct a string, we will use java.lang.String(String s) constructor.
>>> load java.lang.String Hello,_World!!!
Note: as of current release, JavaShell cannot distinguish spaces with escapes, please put a underline _ between Hello and World.
We can confirm that our construction is successful using toString method.
java.lang.String >>> ls
java.lang.String >>> toString
##2. Store the string into the bindings table
We will name the string object str
java.lang.String >>> bind str
##3. Gets the System.out
Getting System.out directly will be somewhat complicated. We will use a help method, sysout in our standard library shown below.
File : JavaShellStandardLibrary.java
public class JavaShellStandardLibrary {
public PrintStream sysout(){
return System.out;
}
//other methods
}We will get the System.out by instantiating our standard library and call the sysout() method.
java.lang.String >>> load JavaShellStandardLibrary
JavaShellStandardLibrary >>> sysout
The sysout method returns a print stream, which is captured in rtObj in our bindings table. We can get it by unbind it from the bindings.
JavaShellStandardLibrary >>> unbind rtObj
java.io.PrintStream >>> ls
##4. Print Hello,_World!!!
We can do this by calling the println(String) method on the System.out stream.
java.io.PrintStream >>> println str
We can see that the Hello,_World is actually printed, among all the messy informations.
##5. Clean it up
Hit Ctrl-D to exit the shell. Create a file under the current directory name MyFirstJavaShellScript.jss, and type in the following command.
File : MyFirstJavaShellScript.jss
load java.lang.String Hello,_World!!
bind str
load JavaShellStandardLibrary
sysout
unbind rtObj
println str
Execute the file with following command.
$ java JavaShell MyFirstJavaShellScript.jss
Hopefully you will see the beautiful Hello,_World!!! output.