/ Development  

Unleash the full potential of your Java skills with the amazing power of JShell

Hi there AppWorks fans,

Welcome to a new installment of AppWorks tips.

The post from last week (the AWP SDK) gave me a deeper insight on Java itself; It also exposed an interesting tool I just want to share in this post…It can benefit anyone who requires to work with Java on the AppWorks platform.


Let get right into it…

How did I even bump into this tool? Well, from last week’s post I tried to call specific Java classes to see what value they bring in my advantage. The classes ran on my local machine via my IDE, but I was wondering what the result of some classes would be running it on my remote AWP VM!? I could just generate a JAR (via maven), upload it to the VM and run the JAR, but I was looking for more editable approach! After Googling, I found a simple solution via the jshell tool delivered with Java itself.

Have yourself a first look; Start you AWP VM (with Java 11), login via MobaXTerm and give this command: /usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.el8_6.x86_64/bin/jshell

jshell_001

Learn the basics of jshell here

After you get yourself familiar with the tool, let’s do a call like this: EIBProperties.getProperties().forEach((key, value) -> {System.out.printf("key: %s; value: %s%n", key, value);});

Hmmm…That’s an error for missing the import for EIBProperties. So, let’s import import com.eibus.util.system.EIBProperties;. That’s another error on finding the package!?

So, can we call jshell with a class-path option? Yes, we can! Let’s start JShell like this:

1
/usr/lib/jvm/java-11-openjdk-11.0.16.0.8-1.el8_6.x86_64/bin/jshell --class-path /opt/opentext/AppWorksPlatform/defaultInst/components/basicutil/basicutil.jar`

You can exit JShell via: /exit

Once you’re in JShell again, import the package, and run our Java-code-line…Just like this:

jshell_002

Niceee! 🤗

Can we also break lines? You mean like this:

jshell_003

O-boy-O-boy…Nice tool…

Can we also read a Java source file and run it? Well, make sure to upload a Java source file (to your home directory on the VM) like for example Simple.java with this content:

1
2
3
4
5
class Simple{  
public static void main(String args[]){
System.out.println("Hello Java");
}
}

In JShell, you can open the file with: /open ~/TestApp.java. Do a verification with /types or /list in JShell itself. To run that main method you can do a call like this: Simple.main(null)
You can even instantiate the ‘Simple’ object Simple simple = new Simple(); and call main from the instance: simple.main(new String[0]).

It’s a party! 🎉 I leave the rest for you to explore…


That’s a nice and quick post…A “DONE” as I may say. It’s always interesting to see how tools can bring actions to your own advantage. Play with it and learn yourself a bit of Java. Have a great weekend…cheers.

Don’t forget to subscribe to get updates on the activities happening on this site. Have you noticed the quiz where you find out if you are also “The AppWorks guy”?