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
This demo was tested in the following deployments:
OpenJDK | GraalJS | Date |
---|---|---|
23.0.1 | 24.1.1 | 2024-12-17 |
22.0.2 | 24.1.1 | 2024-12-17 |
21.0.5 | 24.1.1 | 2024-12-17 |
20.0.2 | 24.1.1 | 2024-12-17 |
19.0.2 | 24.1.1 | 2024-12-17 |
18.0.2 | 24.1.1 | 2024-12-17 |
17.0.13 | 24.1.1 | 2024-12-17 |
Compilation
- 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"
- 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
- Download
SheetJSNashorn.java
:
curl -LO https://docs.sheetjs.com/nashorn/SheetJSNashorn.java
- 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
- Run the command directly:
- Linux/MacOS
- Windows
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
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" -D"polyglot.js.nashorn-compat=true" SheetJSNashorn pres.xlsx
If successful, CSV rows from the first worksheet will be displayed.
Java Archive Test
- Assemble a Java Archive:
jar -cf SheetJSNashorn.jar SheetJSNashorn.class xlsx.full.min.js shim.min.js
- Create new directory and copy the archives and test file:
mkdir -p sheethorn
cp *.jar pres.xlsx sheethorn
cd sheethorn
- Run the program using the Java Archive:
- Linux/MacOS
- Windows
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
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" -D"polyglot.js.nashorn-compat=true" SheetJSNashorn pres.xlsx
This should print the same CSV rows from Step 4.
Footnotes
-
See "Nashorn Compatibility Mode" in the GraalJS documentation. ↩