title: Add directory to classpath at runtime in Java 9
body: <p>I am using OpenOffice API and one of the requirements to prevent the exception <code>java.lang.UnsatisfiedLinkError: com.sun.star.lib.connections.pipe.PipeConnection.createJNI</code> is to add OpenOffice's directory path to classpath. This code below used to work in versions &lt;= Java 8, but it stopped working on Java 9.</p>
  
  <pre><code>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&lt;URLClassLoader&gt; 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);
  }
  </code></pre>
  
  <p>But now it is throwing: <code>java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader</code>. I've looked at <a href="https://stackoverflow.com/a/48323948/2836945">this answer</a> but it is not applicable to my scenario because I'm adding a directory to classpath, not a jar.</p>
  
  <p>How can I fix it?</p>