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
2224.0.02024-03-23
21.0.224.0.02024-03-23
17.0.724.0.12024-04-25

Compilation

  1. Download GraalJS and its dependencies:
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-scriptengine/24.0.1/js-scriptengine-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/js/js-language/24.0.1/js-language-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/polyglot/polyglot/24.0.1/polyglot-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/collections/24.0.1/collections-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/truffle/truffle-api/24.0.1/truffle-api-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/sdk/nativeimage/24.0.1/nativeimage-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/shadowed/icu4j/24.0.1/icu4j-24.0.1.jar"
curl -LO "https://repo1.maven.org/maven2/org/graalvm/regex/regex/24.0.1/regex-24.0.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.2/package/dist/xlsx.full.min.js
curl -LO https://cdn.sheetjs.com/xlsx-0.20.2/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.0.1.jar:js-language-24.0.1.jar:polyglot-24.0.1.jar:collections-24.0.1.jar:truffle-api-24.0.1.jar:nativeimage-24.0.1.jar:icu4j-24.0.1.jar:regex-24.0.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.0.1.jar:js-language-24.0.1.jar:polyglot-24.0.1.jar:collections-24.0.1.jar:truffle-api-24.0.1.jar:nativeimage-24.0.1.jar:icu4j-24.0.1.jar:regex-24.0.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.