Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get jjs --add-opens to work in java9?

I have been using a reflection technique from https://apimeister.com/2015/06/27/add-jar-to-the-classpath-at-runtime-in-jjs.html to load classes at runtime in java's nashorn jjs.

It works in java 8, but in java 9 it doesn't. I know about the recommended command line workaround mentioned in https://stackoverflow.com/a/41265267/5891192

And according to https://stackoverflow.com/a/45970885/5891192 this alternative syntax of using = instead of spaces between the flag and its args seems like it should also be valid (needed because of the nashorn method of passing jvm args through jjs via -J--...

Any hints?


This works... (java 8) ...

$ wget -q http://central.maven.org/maven2/org/apache/poi/poi/4.0.0/poi-4.0.0.jar
$ /usr/lib/jvm/java-1.8.0/bin/jjs -scripting loadit.js -- poi-4.0.0.jar
DONE

This doesn't... (java 9) ...

$ wget -q http://central.maven.org/maven2/org/apache/poi/poi/4.0.0/poi-4.0.0.jar
$ /usr/lib/jvm/java-9/bin/jjs -J--add-opens=java.base/java.net=ALL-UNNAMED -scripting loadit.js -- poi-4.0.0.jar

Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.net.URLClassLoader.addURL(java.net.URL) accessible: module java.base does not "opens java.net" to module jdk.scripting.nashorn.scripts
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:337)...

And here is loadit.js ...

// loadit.js 
function addUrlToClasspath(pth) {
  var s = java.lang.ClassLoader.getSystemClassLoader();
  var C = Java.type("java.lang.Class[]");
  var p = new C(1); p[0]  = java.net.URL.class;
  var m = java.net.URLClassLoader.class.getDeclaredMethod("addURL", p);
  var O = Java.type("java.lang.Object[]"); var a = new O(1); var f = new java.io.File(pth); m.setAccessible(true);
  var u = f.toURL(); a[0] = u; m.invoke(s, a);
}

addUrlToClasspath($ARG[0]);
print("DONE")

Edit: 23 Oct 2018: corrected the "This doesn't (java 9)" example command line

like image 506
J. Brazile Avatar asked Nov 07 '22 00:11

J. Brazile


1 Answers

As I commented above, there are actually 3 problems.

  1. the question as asked - Answer: a. doesn't help (see next point) b. the system level add-opens command line option doesn't make its way to the Nashorn engine used by jjs)
  2. the appending-to-the-system-class-loader approach doesn't work anyway starting with java9 - Java 9, compatability issue with ClassLoader.getSystemClassLoader
  3. starting with java-11, jjs itself is declared deprecated

However, thanks to hints from @Alan , @Holger and my colleague @Philippe , I got what I want with workarounds.

  1. You can create your own URLClassLoader using the desired jars and create a second nashorn engine passing in this classloader (and e.g. command line arguments from jjs).
  2. Add another hack for implementing a so-called "here document" for the script-within-a-script

... and here is a complete example:

// jjs -scripting ora2csv.js -- "select 'hi' from dual" jdbc:oracle:thin:@host:1521:XE user pass

function newJjsEngineWith (jars) {
  var ua = Java.type("java.net.URL[]"); var urls = new ua(jars.length);
  for(var i=0; i<jars.length; i++) {
    var u=new java.net.URL(new java.io.File(jars[i]).toURL());
    urls[i] = u;
  }
  var loader = new java.net.URLClassLoader(urls);
  java.lang.Thread.currentThread().setContextClassLoader(loader);
  var nsef = Java.type("jdk.nashorn.api.scripting.NashornScriptEngineFactory");
  var sa = Java.type("java.lang.String[]"); var args = new sa(2 + $ARG.length);
  args[0] = "-scripting"; args[1] = "--";
  for(var i=0;i<$ARG.length;i++) { args[i+2] = $ARG[i]; }
  return new nsef().getScriptEngine(args, loader);
}

var jjs = newJjsEngineWith(["ojdbc8.jar"]);

function hereDoc(f) { return f.toString().slice(14,-3); }

var code = hereDoc(function(){/*
var q = $ARG[0]; // select 'hello' from dual
var c = $ARG[1]; // jdbc:oracle:thin:@host:1521:XW
var u = $ARG[2]; // username
var p = $ARG[3]; // password
var conn = java.sql.DriverManager.getConnection(c, u, p);
var stmt = conn.createStatement(); rset = stmt.executeQuery(q);
var row=0;
while (rset.next()) {
  row++; var rowBuf = new java.lang.StringBuilder();
  var meta = rset.getMetaData();
  for(var col=0; col< meta.getColumnCount(); col++) {
    var cell = rset.getString(col+1);
    if (cell == null) { cell = ""; }
    rowBuf.append(cell.replaceAll(";",","));
    if (col < meta.getColumnCount()-1) { rowBuf.append(";"); }
  }
  print(rowBuf.toString());
}
stmt.close();
*/});

jjs.eval(code);

This works for java 8 through java 11.

$ /usr/lib/jvm/java-1.8.0/bin/jjs -scripting ora2csv.js -- "select 'hi' from dual" jdbc:oracle:thin:@host:1521:XE user pass
hi
$ /usr/lib/jvm/java-9/bin/jjs -scripting ora2csv.js -- "select 'hi' from dual" jdbc:oracle:thin:@host:1521:XE user pass
hi
$ /usr/lib/jvm/java-10/bin/jjs -scripting ora2csv.js -- "select 'hi' from dual" jdbc:oracle:thin:@host:1521:XE user pass
hi
$ /usr/lib/jvm/java-11/bin/jjs -scripting ora2csv.js -- "select 'hi' from dual" jdbc:oracle:thin:@host:1521:XE user pass
Warning: The jjs tool is planned to be removed from a future JDK release
Warning: Nashorn engine is planned to be removed from a future JDK release
hi

I welcome any suggestions for making this more concise and less pukey

like image 52
J. Brazile Avatar answered Nov 14 '22 02:11

J. Brazile