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
As I commented above, there are actually 3 problems.
However, thanks to hints from @Alan , @Holger and my colleague @Philippe , I got what I want with workarounds.
... 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With