↓ Skip to main content
An ink and watercolour drawing of a closed orange wooden tool chest with teal metal corners and one round keyhole plate. In front of it on the workbench lie two keys: a long orange key with a round bow and a shorter teal key with a square bow, each with a blank paper tag on a string.

Jython or JavaScript: Choosing a Script Language in MAS 9.2

Maximo Automation Scripts - This article is part of a series.
Part 2: This Article
Every automation script starts with one choice: Jython or JavaScript. Both run in Manage, both reach the same Java classes, and both are older than their names suggest. This article shows what each language really is in the MAS 9.2 series, and which one to pick.

The Automation Scripts application asks for the language on the first screen, and the script keeps it for life. IBM’s documents say little about the language itself, beyond a version number that is out of date. So we ran test scripts in both languages on our test system, on the MAS 9.2 series. Every difference on this page was seen there, either when Manage saved a script or when it ran one.

This is the second article of our series on automation scripts. The first article explains what a script is and how Manage runs it.

What ships with Manage
#

Manage brings both engines along. You pick one through the language field of the script.

JythonJavaScript
The languagePython 2.7, running on JavaJavaScript at the ES5 level, running on Java
The engineJython, shipped with ManageNashorn, shipped with Manage
Language names Manage acceptsjython, pythonjavascript, js, nashorn, ecmascript
IBM’s examplesnearly all of thema few

Both engines compile a script when Manage loads it, and both reach the Java classes of Manage. The difference is in the language you write.

Jython is Python 2.7, without its library
#

The Jython in the 9.2 series reports itself as Python 2.7. Three things follow from that, and one surprise.

  • Python 3 syntax does not save. We saved a script with an f-string, and Manage refused it with BMXAA7836E, “The script did not compile”. Use "{0}".format(x), which works.
  • Division of two whole numbers gives a whole number. 7/2 gives 3 and 7/2.0 gives 3.5. Convert one side with float() when you want the fraction, for example in a cost or a ratio.
  • Text and numbers do not mix with +. "a" + 1 raises a TypeError. Wrap the number in str() first.

The surprise is the library. The Python standard library is not there. import json, import re and import datetime each fail with an ImportError. IBM’s best-practice page already says that importing Python modules is not supported, and the test shows how far that goes: even the modules that come with every Python are missing.

Java fills the gap. Jython imports Java classes like Python modules, and those imports work:

from java.util.regex import Pattern
from java.util import Calendar
from com.ibm.json.java import JSONObject

body = JSONObject()
body.put("wonum", mbo.getString("WONUM"))
responseBody = body.serialize()

com.ibm.json.java is the JSON library that IBM’s own script examples use, and it ships with Manage.

JavaScript is ES5
#

The JavaScript engine in Manage runs the language level of ES5. Everything newer either does not save or is missing when the script runs.

  • Newer syntax does not save. A script with let was refused with the same BMXAA7836E. Arrow functions, template strings, classes, default parameters, destructuring, spread and for...of fail the same way. Write var, function and string concatenation.
  • Newer built-ins are missing. Map, Set, Promise, Symbol, Object.assign, Array.prototype.includes, Array.prototype.find and String.prototype.startsWith are all undefined at run time. The script saves, and it fails only on the line that uses one.
  • JSON is there. JSON.parse and JSON.stringify work, which is the strongest reason to use JavaScript for scripts that read or write JSON.
  • Never write "use strict". A script returns its results by assigning variables such as responseBody that it never declares. Strict mode forbids exactly that, and the assignment throws a ReferenceError.

The ES5 rule matters most when code comes from somewhere else. A snippet from a current JavaScript project will almost always use let, an arrow function or a template string, so it fails at save.

Reaching Java from a script
#

Both languages call Java, and both need the class first. In Jython that is an import. In JavaScript, Java.type loads the class.

from psdi.server import MXServer

assets = MXServer.getMXServer().getMboSet("ASSET", mbo.getUserInfo())
var MXServer = Java.type("psdi.server.MXServer");

var assets = MXServer.getMXServer().getMboSet("ASSET", mbo.getUserInfo());

In JavaScript, MXServer is no global: without the Java.type line it is undefined. Remember to close a set that you open this way. Article 17 of this series covers why.

Lists behave differently. In Jython, a Python list is converted when a Java method expects a list, so you can hand one over as it is. In JavaScript, an array stays a JavaScript object, and Java sees no list. Java.to converts it:

// a Java list
var keys = Java.to(["NAME"], "java.util.List");
// a Java string array
var names = Java.to(["NAME"], "java.lang.String[]");

This matters wherever Manage reads a list back from the script. A lookup script, for example, sets srcKeys and targetKeys, and Manage expects a Java list or a Java string array there. IBM’s JavaScript example for lookups sets both as plain arrays. Convert them with Java.to.

Which one to pick
#

There is no wrong choice, because both languages reach the same objects. Our view, from the differences above:

  • Pick Jython for object, attribute, action and condition scripts, and wherever your team keeps existing scripts. Nearly all of IBM’s examples are Jython, so the code in IBM’s pages works as written.
  • Pick JavaScript for scripts that read or write JSON, such as REST API scripts and integration scripts that handle JSON messages. JSON is built in, and Jython has to use the Java library.
  • Pick JavaScript also when your scripts are generated from TypeScript. Article 32 of this series shows how that works with the ES5 limit.

Whichever you pick, keep to it inside one group of scripts that call each other. A reader of the code then has one set of rules to keep in mind.

What IBM’s documents get wrong here
#

Both IBM documents name Jython 2.7.2, and the scripting guide says that Nashorn comes with the Java runtime. Neither is true for the 9.2 series:

IBM’s documentsWhat we found on the MAS 9.2 series
Jython2.7.22.7.4
Nashorncomes with the Java runtimeshipped with Manage, because Java has had no Nashorn since Java 15
JavaScript levelnot statedES5

IBM’s debugging example also adds a decimal number to a text with +, in Jython. It fails with a TypeError of the same kind as the one in our test. Article 33 of this series collects the corrections that matter most.

What comes next
#

Article 3 of this series shows how scripts, launch points and variables fit together in practice.

IBM’s own pages are the reference to keep next to this series:

The complete list of corrections, with the evidence for each one, is in our member area. Members get access on request, so get in touch if you want it. If you want help with the scripts in your own Manage, our services page shows how we work.

IBM and Maximo are trademarks of International Business Machines Corporation, registered in many jurisdictions worldwide.

Maximo Automation Scripts - This article is part of a series.
Part 2: This Article