Discussion
Pegasystems Inc.
JP
Last activity: 12 May 2021 6:24 EDT
How to call OS command from an activity
Hi,
In the past, I shared how to call an activity from batch file (https://collaborate.pega.com/discussion/how-call-activity-batch-file). In this post I am sharing the opposite - how to call batch file from an activity. Batch (Windows), Shell (UNIX), or any OS command can be run using this approach.
In standard Java, Runtime.getRuntime() or new ProcessBuilder are available for use to call OS command. Until Pega 8.2, things were simple - we could write these in the activity directly. However, unfortunately starting with 8.3, these were banned to use in PRPC for security reasons (Java injection vulnerabilities). Prohibited code includes:
- Runtime.getRuntime()
- new ProcessBuilder()
- JavaCompiler
- org.dita.dost.invoker
If you try it, PRPC throws an error both in design time and run time. For example, below code is to run calculator program on Windows and it was valid in 8.2. From 8.3 onwards, it won't even let you save the rule.
However, sometimes customer may still require such implementation per their business needs. Workaround is to to write a Java code outside of PRPC, package it to Java Archive (Jar), import it into Pega database, and call it from an activity. This way, you can still achieve the same.
Steps:
- Using Java IDE, write a Java program and compile it to make class files. Then package them up into a single Jar file.
- From Dev Studio, using the regular Import tool, import the Jar file into Pega database. It goes to PR_ENGINECLASSES table in the rules schema.
- Restart the application server so that your custom class in Jar gets loaded in the bootstrap process.
- Create an activity and call its method.
I have documented How-to as attached. Hope this helps.
Thanks,