1

I am fairly new to java and just can't get my java application which relies of external libraries working...

I have two libraries and an application, all exported from eclipse as jars. NOTE: I don't want them all exported into the same jar, as I wish to re-use the two libraries elsewhere.

  1. enix.lib.common.jar (library 1)
  2. enix.lib.events.jar (library 2)
  3. enix.cmd.events.jar (console application)

When I run:

java -jar enix.cmd.events.jar

I get:

Exception in thread "main" java.lang.NoClassDefFoundError: enix/lib/events/errors/EventLogNotAvailableException

Which obviously means it doesn't have the path to the enix.lib.events.jar (which if renamed to a zip file contains the file enix/lib/events/errors/EventLogNotAvailableException.class) - I then set the classpath in various ways like so:

java -cp ".;*.jar;enix.lib.events.jar" -jar enix.cmd.events.jar

But I get the same error. :( I also have a GUI app called enix.gnome.events.jar which relies on various jars in /usr/share/java and /opt/libs/jars.

Could someone please explain what I am getting wrong and why, I would be most grateful! THANKS!

tommed
  • 1,521
  • 2
  • 19
  • 33
  • 1
    This looks like your answer: http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath – nwinkler Mar 13 '12 at 19:57
  • Thanks, but event including the jar explicitly by filename doesn't seem to work (see above again) - will try again without the wildcards, but I have a feeling something else is happening to prevent this working..? – tommed Mar 14 '12 at 13:54

2 Answers2

2

*.jar doesn't work, unfortunately. You can use wildcards in java classpaths, but only by putting all your jars in a directory and telling the classpath to use every jar that's there (see this).

I recommend, though, just listing out every jar that you need explicitly. Conventionally you'll see that most applications list every single jar.

Roy Truelove
  • 22,016
  • 18
  • 111
  • 153
  • But I have explicitly mentioned enix.lib.events.jar in the classpath and it stil doesn't work? Any ideas? – tommed Mar 14 '12 at 13:51
  • Can you post your new command? Are you running on windows or UNIX? Try replacing the semicolon with a colon. – Roy Truelove Mar 14 '12 at 17:58
  • Linux. This still throws an error: java -cp "enix.lib.events.jar" -jar enix.cmd.events.jar – tommed Mar 18 '12 at 18:42
1

When compiling/running java apps with multiple jars, I've found the easiest way is to just add the applicable jars right to my classpath. That way, when you compile or run the program, all of the applicable jars are available.

See http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html for setting the classpath in Windows/Unix/Linux.

Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • I will try this and see if it fixes the problem, though I think something more subtle is causing my problem. Have awarded your answer with a point regardless as this technique is good to know, thanks! – tommed Mar 14 '12 at 13:58
  • CLASSPATH="enix.lib.events.jar" java -jar enix.cmd.events.jar - still breaks on Ubuntu :( – tommed Mar 18 '12 at 18:44
  • @tommed - did you include the other jars in your classpath (enix.lib.common.jar & enix.lib.events.jar & whatever other ones...)? Also, what does `echo $CLASSPATH` bring back from your Linux shell? – Zack Macomber Mar 19 '12 at 12:32