0

I've created a library, let's call it MyLib. This library, MyLib, uses Google Guava library, so I've Guava's jar in the library folder that belongs to MyLib.

Now I've created a new project where I'll use MyLib. I've imported MyLib to my project and when I run it I get this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/eventbus/EventBus
    at com.dwnz.mylib.<init>(SchoolBus.java:29)
    at com.dwnz.mylib.getAsyncBusToMainThread(SchoolBus.java:61)
    at view.MainFrame.<init>(MainFrame.java:91)
    at view.MainFrame.newInstance(MainFrame.java:586)
    at view.Main.main(Main.java:98)
Caused by: java.lang.ClassNotFoundException: com.google.common.eventbus.EventBus
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more
Java Result: 1

I've googled and I found out that on run time JVM can't load the class. If I add Google's Guava jar as library to my project I don't get this exception.

What I don't understand here is why can't JVM load Guava's class from MyLib library if I've added Guava jar to MyLib library? How can I make it work this way? Otherwise I find it a bit pointless to create this library.

I'm using NetBeans, if it makes any difference.

dazito
  • 7,740
  • 15
  • 75
  • 117
  • You either use a dependency management tool like maven , or setup the classpath manually to include the guava jars needed at runtime. Either way the classpath governs what jars are loaded at runtime. – Bhaskar Jul 22 '14 at 22:21
  • are you using guava directly in your project that uses `MyLib`? – Xinzz Jul 22 '14 at 22:32
  • 1
    possible duplicate of [Classpath including JAR within a JAR](http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar) – Scheintod Jul 22 '14 at 22:40
  • @Xinzz If I dont add the jar from Guava to my project I get that exception. If I do add the guava jars in my project I dont't get the exception. – dazito Jul 22 '14 at 22:59
  • that wasn't the question... are you using any of Guava's classes or methods DIRECTLY from the project that uses the library `MyLib` – Xinzz Jul 22 '14 at 23:06
  • @Xinzz No, I'm not. All I call are methods from MyLib – dazito Jul 22 '14 at 23:12
  • @Bhaskar can you point me into a tutorial on how to do it? I'm lost here, been googling but I'm afraid I'm not on the right path – dazito Jul 22 '14 at 23:28
  • I've came up to this tutorial, quite easy to do my task. http://www.tech-recipes.com/rx/39256/add-dependencies-to-maven-pom-xml-file/ – dazito Jul 22 '14 at 23:53

0 Answers0