Skip to main content

Data Processing with Nashorn

Nashorn is a JS engine for Java. It shipped with Java distributions starting with Java 8 and was eventually removed in Java 15. The project was spun off and a compatible standalone release is available for Java 15+.

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.

Integration Details

Initialize Nashorn

Nashorn does not provide a global variable. It must be created:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;

/* initialize nashorn engine */
ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("javascript");

/* create global */
engine.eval("var global = (function(){ return this; }).call(null);");

Load SheetJS Scripts

The SheetJS Standalone scripts can be parsed and evaluated in a Nashorn context.

The main library can be loaded by reading the script from the file system and evaluating in the Nashorn context:

engine.eval(new Scanner(
SheetJS.class.getResourceAsStream("/xlsx.full.min.js")
).useDelimiter("\\Z").next());

To confirm the library is loaded, XLSX.version can be printed using the Nashorn print built-in:

engine.eval("print('SheetJS Version ' + XLSX.version);");

Reading Files

Nashorn does not properly project byte[] into a JS array or Int8Array. The recommended workaround is to copy the data in the JS context using the JS code:

function b2a(b) {
var out = typeof Uint8Array == 'function' ? new Uint8Array(b.length) : new Array(b.length);
/* `b` is similar to Int8Array (values in the range -128 .. 127 ) */
for(var i = 0; i < out.length; i++) out[i] = (b[i] + 256) & 0xFF;
return out;
}

This function should be embedded in the Java code:

/* read spreadsheet bytes */
engine.put("bytes", Files.readAllBytes(Paths.get(args[0])));

/* convert signed byte array to JS Uint8Array or unsigned byte array */
engine.eval(
"function b2a(b) {" +
"var out = typeof Uint8Array == 'function' ? new Uint8Array(b.length) : new Array(b.length);" +
"for(var i = 0; i < out.length; i++) out[i] = b[i] & 0xFF;" +
"return out;" +
"}" +
"var u8a = b2a(bytes)"
);

/* parse workbook */
engine.eval("var wb = XLSX.read(u8a, {type: 'array'})");

Complete Example

Tested Deployments

This demo was tested in the following deployments:

OpenJDKNashornDate
23.0.115.4 standalone2024-12-17
22.0.215.4 standalone2024-12-17
21.0.515.4 standalone2024-12-17
20.0.215.4 standalone2024-12-17
19.0.215.4 standalone2024-12-17
18.0.215.4 standalone2024-12-17
17.0.1315.4 standalone2024-12-17
16.0.115.4 standalone2024-12-17
15.0.1015.4 standalone2024-12-17
14.0.2Built-in2024-12-17
13.0.14Built-in2024-12-17
12.0.2Built-in2024-12-17
11.0.25Built-in2024-12-17
10.0.2Built-in2024-12-17
9Built-in2024-12-17
1.8.0Built-in2024-12-17

Compilation

Nashorn is available without additional dependencies

  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 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:

Due to Java path inconsistencies, the command depends on the operating system:

java -cp ".:SheetJSNashorn.jar" SheetJSNashorn pres.xlsx

This should print the same CSV rows from Step 4.