How to Use JShell (Java REPL) in Combination with UiPath

Since Release SE 9 Java offers the JShell, a Java read-eval-print-loop (REPL) tool to interactively evaluate Java declarations, statements and expressions. But JShell offers more as try out code and explore Java language options. Also you can load a JShell script file, which is a sequence of snippets and JShell commands, which is executed by JShell.

This means that we can execute Java command sequences as script. This integration scenario offers us the advantage that we do not have to deliver binary files, such as JAR or Class, but we can deliver the source code here. To realize a smart integration of JShell into UiPath I have developed an activity.

JShell.Activities.2.1.0.nupkg (15.0 KB)

After installing the package you can find the “Invoke JShell File” item in the App Invoker > Java path. Also it is necessary to install the OpenJDK and to set the JAVA_HOME variable, if you like, otherwise you have to set the path in the option JShellPath.

image

With this it is possible to use JShell scripts.

image

image

Here the arguments in VB convention.

"-R-Darg1=""Hello World"" -R-Darg2=""Hello Stefan"""

Here a tiny example which reads these input arguments and delivers it back.

//-Begin----------------------------------------------------------------

String arg1 = System.getProperty("arg1");
System.out.println(arg1);

String arg2 = System.getProperty("arg2");
System.out.println(arg2);

/exit

//-End------------------------------------------------------------------

image

On this way we can seamlessly execute Java source code as JShell script into UiPath. It is not necessary to compile the source code. For certain use cases this can be very advantageous.

You can find the JShell User Guide here.

Addendum 24.09.2022: Current version of this activity works in Windows - Legacy (x86), Windows (x64) and Cross-platform (x64) compatibiltiy mode.

Here the result of the (unit) test, to check the activity with an independent console application, in a Linux environment.

OpenJDK as NuGet Package

Another way of deployment is to build an individual NuGet package which contains the OpenJDK. However, it should be considered whether this approach is conform with an existing software distribution system.

On this way it is possible to use the OpenJDK direct in the context of the UiPath project.

image

Additional, to ensure that the standard environment variable JAVA_HOME is set correctly, here a tiny snippet to handle this.

//-UiPath Begin---------------------------------------------------------

try {

  JAVA_HOME = Environment.GetEnvironmentVariable("JAVA_HOME");
  if(String.IsNullOrEmpty(JAVA_HOME)) {
    string UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string JDKPath = UserProfile + "\\.nuget\\packages\\openjdk\\16.0.1";
    Environment.SetEnvironmentVariable("JAVA_HOME", JDKPath);
    JAVA_HOME = Environment.GetEnvironmentVariable("JAVA_HOME");
  }

} catch (Exception e) {
  System.err.println(e.toString());
  return;
}

//-UiPath End-----------------------------------------------------------

image

With all these steps is the integration of Java language in an UiPath workflow easier possible.

You can find an example how to use Java Selenium, based on this approach, here.

6 Likes