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-1.8.0/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");
Aucun commentaire:
Enregistrer un commentaire