Skip to main content

Data Processing with GraalJS

GraalJS is a JS engine for Java. The project offers a JSR-223 compliant OpenJDK-compatible build.

SheetJS is a JavaScript library for reading and writing data from spreadsheets.

The "Complete Example" section includes a complete Java command-line tool for reading data from spreadsheets and printing CSV rows.

The Java packages used in this demo are covered under open source licenses. The Universal Permissive License covers most of the packages, while icu4j uses a different, yet still permissive, license.

Integration Details

When this demo was last tested, raw byte arrays could not be passed to GraalJS.

This is a limitation of the default GraalJS behavior.

Instead, this demo uses Nashorn Compatibility Mode1 through a runtime flag:

java -Dpolyglot.js.nashorn-compat=true ...

The Nashorn demo code and explanation applies to JSR-223 compatible ScriptEngine implementations, including GraalJS.

Complete Example

Tested Deployments

This demo was tested in the following deployments:

OpenJDKGraalJSDate
23.0.124.1.12024-12-17
22.0.224.1.12024-12-17
21.0.524.1.12024-12-17
20.0.224.1.12024-12-17
19.0.224.1.12024-12-17
18.0.224.1.12024-12-17
17.0.1324.1.12024-12-17

Compilation

  1. Download GraalJS and its dependencies:
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-scriptengine/24.1.1/js-scriptengine-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-language/24.1.1/js-language-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/polyglot/polyglot/24.1.1/polyglot-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/collections/24.1.1/collections-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/truffle/truffle-api/24.1.1/truffle-api-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/nativeimage/24.1.1/nativeimage-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/shadowed/icu4j/24.1.1/icu4j-24.1.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/regex/regex/24.1.1/regex-24.1.1.jar"
  1. Download the SheetJS Standalone script, shim script and test file. Move all three files to the project directory:
curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/shim.min.js
curl -LO https://docs.sheetjs.com/pres.xlsx
  1. Download SheetJSNashorn.java:
curl -LO https://docs.sheetjs.com/nashorn/SheetJSNashorn.java
  1. Build the sample class:
javac SheetJSNashorn.java

This program tries to parse the file specified by the first argument and prints CSV rows from the first worksheet.

Standalone Test

  1. Run the command directly:
java -cp ".:js-scriptengine-24.1.1.jar:js-language-24.1.1.jar:polyglot-24.1.1.jar:collections-24.1.1.jar:truffle-api-24.1.1.jar:nativeimage-24.1.1.jar:icu4j-24.1.1.jar:regex-24.1.1.jar" -Dpolyglot.js.nashorn-compat=true SheetJSNashorn pres.xlsx

If successful, CSV rows from the first worksheet will be displayed.

Java Archive Test

  1. Assemble a Java Archive:
jar -cf SheetJSNashorn.jar SheetJSNashorn.class xlsx.full.min.js shim.min.js
  1. Create new directory and copy the archives and test file:
mkdir -p sheethorn
cp *.jar pres.xlsx sheethorn
cd sheethorn
  1. Run the program using the Java Archive:
java -cp ".:js-scriptengine-24.1.1.jar:js-language-24.1.1.jar:polyglot-24.1.1.jar:collections-24.1.1.jar:truffle-api-24.1.1.jar:nativeimage-24.1.1.jar:icu4j-24.1.1.jar:regex-24.1.1.jar:SheetJSNashorn.jar" -Dpolyglot.js.nashorn-compat=true  SheetJSNashorn pres.xlsx

This should print the same CSV rows from Step 4.

Footnotes

  1. See "Nashorn Compatibility Mode" in the GraalJS documentation.