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 510805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:09:04+00:00 2026-05-13T07:09:04+00:00

I’m writing a library that needs to have some code if a particular library

  • 0

I’m writing a library that needs to have some code if a particular library is included. Since this code is scattered all around the project, it would be nice if users didn’t have to comment/uncomment everything themselves.

In C, this would be easy enough with a #define in a header, and then code blocks surrounded with #ifdefs. Of course, Java doesn’t have the C preprocessor…

To clarify – several external libraries will be distributed with mine. I do not want to have to include them all to minimize my executable size. If a developer does include a library, I need to be able to use it, and if not, then it can just be ignored.

What is the best way to do this in Java?

  • 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-13T07:09:05+00:00Added an answer on May 13, 2026 at 7:09 am

    As other have said, there is no such thing as #define/#ifdef in Java. But regarding your problem of having optional external libraries, which you would use, if present, and not use if not, using proxy classes might be an option (if the library interfaces aren’t too big).

    I had to do this once for the Mac OS X specific extensions for AWT/Swing (found in com.apple.eawt.*). The classes are, of course, only on the class-path if the application is running on Mac OS. To be able to use them but still allow the same app to be used on other platforms, I wrote simple proxy classes, which just offered the same methods as the original EAWT classes. Internally, the proxies used some reflection to determine if the real classes were on the class-path and would pass through all method calls. By using the java.lang.reflect.Proxy class, you can even create and pass around objects of a type defined in the external library, without having it available at compile time.

    For example, the proxy for com.apple.eawt.ApplicationListener looked like this:

    public class ApplicationListener {
    
        private static Class<?> nativeClass;
    
        static Class<?> getNativeClass() {
            try {
                if (ApplicationListener.nativeClass == null) {
                    ApplicationListener.nativeClass = Class.forName("com.apple.eawt.ApplicationListener");
                }
    
                return ApplicationListener.nativeClass;
            } catch (ClassNotFoundException ex) {
                throw new RuntimeException("This system does not support the Apple EAWT!", ex);
            }
        }
    
        private Object nativeObject;
    
        public ApplicationListener() {
            Class<?> nativeClass = ApplicationListener.getNativeClass();
    
            this.nativeObject = Proxy.newProxyInstance(nativeClass.getClassLoader(), new Class<?>[] {
                nativeClass
            }, new InvocationHandler() {
    
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    String methodName = method.getName();
    
                    ApplicationEvent event = new ApplicationEvent(args[0]);
    
                    if (methodName.equals("handleReOpenApplication")) {
                        ApplicationListener.this.handleReOpenApplication(event);
                    } else if (methodName.equals("handleQuit")) {
                        ApplicationListener.this.handleQuit(event);
                    } else if (methodName.equals("handlePrintFile")) {
                        ApplicationListener.this.handlePrintFile(event);
                    } else if (methodName.equals("handlePreferences")) {
                        ApplicationListener.this.handlePreferences(event);
                    } else if (methodName.equals("handleOpenFile")) {
                        ApplicationListener.this.handleOpenFile(event);
                    } else if (methodName.equals("handleOpenApplication")) {
                        ApplicationListener.this.handleOpenApplication(event);
                    } else if (methodName.equals("handleAbout")) {
                        ApplicationListener.this.handleAbout(event);
                    }
    
                    return null;
                }
    
            });
        }
    
        Object getNativeObject() {
            return this.nativeObject;
        }
    
        // followed by abstract definitions of all handle...(ApplicationEvent) methods
    
    }
    

    All this only makes sense, if you need just a few classes from an external library, because you have to do everything via reflection at runtime. For larger libraries, you probably would need some way to automate the generation of the proxies. But then, if you really are that dependent on a large external library, you should just require it at compile time.

    Comment by Peter Lawrey: (Sorry to edit, its very hard to put code into a comment)

    The follow example is generic by method so you don’t need to know all the methods involved. You can also make this generic by class so you only need one InvocationHandler class coded to cover all cases.

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        ApplicationEvent event = new ApplicationEvent(args[0]);
        Method method = ApplicationListener.class.getMethod(methodName, ApplicationEvent.class);
        return method.invoke(ApplicationListener.this, event);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 245k
  • Answers 245k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Concept Really it's about code design. E.g. whether to use… May 13, 2026 at 8:13 am
  • Editorial Team
    Editorial Team added an answer You should use something like the 960 Grid System. It… May 13, 2026 at 8:13 am
  • Editorial Team
    Editorial Team added an answer Sounds like you need a templating engine for your app.… May 13, 2026 at 8:13 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.