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 |
---|---|---|
22 | 24.0.1 | 2024-05-25 |
21.0.3 | 24.0.1 | 2024-05-25 |
20.0.2 | 24.0.1 | 2024-05-25 |
19.0.2 | 24.0.1 | 2024-05-25 |
18.0.2 | 24.0.1 | 2024-05-25 |
17.0.10 | 24.0.1 | 2024-05-25 |
Compilation
- 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"
- 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.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
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" -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.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
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" -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. ↩