4

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?

lucasdc
  • 1,032
  • 2
  • 20
  • 42
  • AFIK Open Office is written in '32 bits code'. I'm not sure is there a JDK9 available other than in 64 bits? Maybe i totally misunderstood all of this? – zlakad Jan 31 '18 at 18:27
  • I forgot to say that I'm running this code via Java Web Start (.jnlp file). This code is needed to make it work with OpenOffice API in versions under or equals Java 8 (ONLY in 32 bits as @zlakad stated), but in Java 9 I am getting ClassCastException. I have not noticed about Java 9 being 64 bits only (maybe OpenOffice API cannot be used this way anymore and I'm not aware of it). – lucasdc Jan 31 '18 at 18:39
  • are you sure, this **EXACT** code was/is working fine with Java 8 ? – Ravi Jan 31 '18 at 18:53
  • 1
    yes, I am sure. – lucasdc Jan 31 '18 at 19:13
  • 7
    Yes, since Java9, the application class loader is not a subclass of `URLClassLoader` anymore (which always was an unspecified implementation detail). The solution of the linked answer works regardless of whether the URL points to a jar file or a directory. – Holger Jan 31 '18 at 19:56
  • But what should I use in "MainClassOfJar"? The code states: Class.forName("MainClassOfJar", true, loader); – lucasdc Jan 31 '18 at 20:00
  • Can you tell us more about the code that throws com.sun.star.comp.helper.BootstrapException? Does it have a static reference to the class in the directory or does it use Class.forName to load it? If the latter then the answer cited in the other question is the right way to fix this. – Alan Bateman Feb 01 '18 at 09:02
  • 2
    Well, you could simply replace `Bootstrap.class.getClassLoader()` with the `URLClassLoader` you’ve just created when following the linked answer. Though, when replacing the entire lookup with the result `File`, you don’t need to do that at all. But we can’t help you on the CPU architecture issue. Note that while Oracle decided not to publish 32 bit builds, there is no fundamental problem with them and it’s open source, so you could look for alternative publishers or build a 32 bit version yourself. – Holger Feb 01 '18 at 14:46

0 Answers0