I am using OpenOffice API and one of the requirements to prevent the exception java.lang.UnsatisfiedLinkError: com.sun.star.lib.connections.pipe.PipeConnection.createJNI
is to add OpenOffice's directory path to classpath. This code below used to work in versions <= Java 8, but it stopped working on Java 9.
try {
String path = "C:/Program Files (x86)/OpenOffice 4/program/";
File f = new File(path);
URL u = f.toURI().toURL();
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u});
} catch(Exception e) {
Utils.logger.error(e.getMessage(), e);
}
But now it is throwing: java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader
. I've looked at this answer but it is not applicable to my scenario because I'm adding a directory to classpath, not a jar.
How can I fix it?