Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:10:12+00:00 2026-05-16T03:10:12+00:00

How do I call a method of a class dynamically + conditionally? (Class is

  • 0

How do I call a method of a class dynamically + conditionally?
(Class is eventually not in classpath)

Let’s say, I need the class NimbusLookAndFeel, but on some systems it’s not available (i.e. OpenJDK-6).

So I must be able to:

  • Get to know it that class is available (at runtime),
  • If it’s not the case, skip the whole thing.
  • How do I manage to override a method of a dynamically-loaded class
    (thus creating an anonymous inner sub-class of it)?

Code example

public static void setNimbusUI(final IMethod<UIDefaults> 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;
    }

  });
}

EDIT:
Now I edited my code, as it was suggested, to intercept NoClassDefFoundError using try-catch. It fails. I don’t know, if it’s OpenJDK’s fault. I get InvocationTargetException, caused by NoClassDefFoundError. Funny, that I can’t catch InvocationTargetException: It’s thrown anyway.

EDIT2::
Cause found: I was wrapping SwingUtilities.invokeAndWait(...) around the tested method, and that very invokeAndWait call throws NoClassDefFoundError when loading Nimbus fails.

EDIT3::
Can anyone please clarify where NoClassDefFoundError can occur at all? Because it seems that it’s always the calling method, not the actual method which uses the non-existing class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:10:13+00:00Added an answer on May 16, 2026 at 3:10 am

    Get to know it that class is available (at runtime)
    Put the usage in a try block …

    If it’s not the case, skip the whole thing
    … and leave the catch block empty (code smell?!).

    How do I manage to override a method of a dynamically-loaded class
    Just do it and make sure the compile-time dependency is satisfied. You are mixing things up here. Overriding takes place at compile time while class loading is a runtime thing.

    For completeness, every class you write is dynamically loaded by the runtime environment when it is required.

    So your code may look like:

    public static void setNimbusUI(final IMethod<UIDefaults> method)
        throws UnsupportedLookAndFeelException {
    
        try {
            // NimbusLookAndFeel may be now available
            UIManager.setLookAndFeel(new NimbusLookAndFeel() {
    
                @Override
                public UIDefaults getDefaults() {
                    final UIDefaults defaults = super.getDefaults();
                    method.perform(defaults);
                    return defaults;
                }
    
            });
       } catch (NoClassDefFoundError e) {
           throw new UnsupportedLookAndFeelException(e);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I need to dynamically call a class and a method of that class.
how can I call a method from another class and not be restriced to
How to call Html.Partial() method from controller or some other method in external class
I am trying to call a class method dynamically depending on a condition. This
In groovy, how do you dynamically call a static method of a class? void
I want to dynamically call the method of a custom class much like the
I am in a situation where I need to call a Class method and
I call addNotify() method in class that I posted here. The problem is, that
How do I call a method from a class in a ruby gem? i.e.
I want to call a method of my class inside a lambda expression: void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.