title: Java: How do I override a method of a class dynamically (class is eventually NOT in classpath)?
body: <p>How do I call a method of a class dynamically + conditionally?<br />
  <em>(Class is eventually <strong>not</strong> in classpath)</em></p>
  <p>Let's say, I need the class <code>NimbusLookAndFeel</code>, but on some systems it's not available (i.e. <code>OpenJDK-6</code>).</p>
  <p>So I must be able to:</p>
  <ul>
  <li>Get to know it that class is <strong>available</strong> (at runtime),</li>
  <li>If it's not the case, <strong>skip</strong> the whole thing.</li>
  <li>How do I manage to <strong>override a method of a dynamically-loaded class</strong><br />
  (thus creating an anonymous inner sub-class of it)?</li>
  </ul>
  <h2>Code example</h2>
  <pre><code>public static void setNimbusUI(final IMethod&lt;UIDefaults&gt; method)
      throws UnsupportedLookAndFeelException {
  
    // NimbusLookAndFeel may be now available
    UIManager.setLookAndFeel(new NimbusLookAndFeel() {
  
      @Override
      public UIDefaults getDefaults() {
        UIDefaults ret = super.getDefaults();
        method.perform(ret);
        return ret;
      }
  
    });
  }
  </code></pre>
  <p><strong>EDIT</strong>:<br />
  Now I edited my code, as it was suggested, to intercept <code>NoClassDefFoundError</code> using try-catch. It fails. I don't know, if it's OpenJDK's fault. I get <code>InvocationTargetException</code>, caused by <code>NoClassDefFoundError</code>. Funny, that I <strong>can't</strong> catch <code>InvocationTargetException</code>: It's thrown anyway.</p>
  <p><strong>EDIT2:</strong>:<br />
  Cause found: I was wrapping <code>SwingUtilities.invokeAndWait(...)</code> around the tested method, and that very <code>invokeAndWait</code> call throws <code>NoClassDefFoundError</code> when loading Nimbus fails.</p>
  <p><strong>EDIT3:</strong>:<br />
  Can anyone please clarify <strong>where</strong> <code>NoClassDefFoundError</code> can occur at all? Because it seems that it's always the calling method, not the actual method which uses the non-existing class.</p>