0

Example:
This would exist in the JAR.

public class Foo {
    public void saySomething() {
        Log.d("Status","Got it!");
    }
}

This would exist in my Main class.

public class Main {
    public void tryToGetSomething() {
        Foo.saySomething();
    }
}

Obviously, the problem with that is that it would throw an error if the JAR was NOT included. How do I get around this? How can I protectively code this so that it will execute this if the JAR is included, but do something else if the JAR is NOT included?

Also, sorry if the code doesn't make sense, just wrote it in a few seconds. You should get the idea.

Hulabaloo
  • 57
  • 6
  • you could use Reflections to dynamically create an object of that class and do stuff with it if. If that class isn't present, you could do something else. See Class.forName(String). – Alexander Feb 13 '15 at 19:43
  • I guess you could do something like [this](http://stackoverflow.com/questions/482633/in-java-is-it-possible-to-know-whether-a-class-has-already-been-loaded). But anyway I don't quite see why you should protect yourself for a jar that is not included. I guess it will depend on the context, but most of the times you could _predict_ which jars have been included. – lrnzcig Feb 13 '15 at 19:47
  • The JAR being included is completely optional to the user. If the JAR isn't included we want "Main" to function normally. Thank you to both for your answers. – Hulabaloo Feb 13 '15 at 19:53

2 Answers2

1

If you try and use a class/method that does not exist, a java.lang.NoClassDefFoundError will be thrown. You could catch this error and provide alternate behavior in this case:

try {
  Foo.saySomething();
} catch (NoClassDefFoundError e) {
  System.out.println("Load the jar");
}

Interestingly, you won't be able to catch the ClassNotFoundException that is also thrown due to it being a checked exception. It might be worth checking out this post for details on how they differ to make sure you are addressing the appropriate issue.

Community
  • 1
  • 1
Steve U
  • 326
  • 2
  • 7
  • Though I chose the other answer, this answer is still very appreciated. I will try this method as well. Thank you for your time! – Hulabaloo Feb 13 '15 at 20:02
  • I've just tried this, but my java compiler (oracle jdk 8) told me this is an unreachable catch block (as if!!) but it works for me when instead catching a Throwable. – kaefert Jan 29 '16 at 15:08
1

The following reflection code to lookup the Foo class and invoke the method will work. You will get an exception when the jar is not imported, which you can handle gracefully.

try {
    Class clazz = Class.forName("Foo");
    Method method = clazz.getMethod("saySomething");
    Object obj = clazz.newInstance();
    method.invoke(obj);
 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 }