0

So I have a program that is using Joda-Time that works perfectly fine when run through Eclipse. However when I export it as a .jar, the program doesn't run at all. When I ran the program through command prompt, I got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/DateTime
    at BouncyBallSource.BouncyBallV5.<init>(BouncyBallV5.java:131)
    at BBDriver.main(BBDriver.java:17)
Caused by: java.lang.ClassNotFoundException: org.joda.time.DateTime
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

I added the Joda-Time jar to the Build Path, and I get no errors in Eclipse. I have no clue why this is happening, so how do I go about fixing it, and is anyone else having this problem?

By the way, line 131 is: DateTime dt = new DateTime();

Command: java -jar BouncyBallV5.0.0A2.jar

Ryan
  • 727
  • 2
  • 14
  • 31
  • Can you provide the command you are using to launch the application? – bradimus Nov 07 '16 at 21:59
  • and also we need the know where the Joda-Time jar is located relative to the CWD you try to start from. – Timothy Truckle Nov 07 '16 at 22:03
  • @bradimus it's up – Ryan Nov 07 '16 at 22:03
  • 3
    You need to add the Joda-Time jar to the class path. This should help: http://stackoverflow.com/questions/18413014/run-a-jar-file-from-the-command-line-and-specify-classpath – bradimus Nov 07 '16 at 22:04
  • @bradimus Sorry if I don't understand, I'm fairly new to java, but why is that necessary? And is there anyway I can avoid doing that, so the jar is easy to run? I know I can create a .bat file that can run the jar with the specified arguments, but I'd much rather have just a single straight forward jar – Ryan Nov 07 '16 at 22:13
  • What's the full name of your `main` method (including package and class)? Looks like it might be ` BouncyBallSource.BBDriver.main` Since you asked about `.bat`, I assume you are using Windows. Is that correct? – bradimus Nov 07 '16 at 22:17
  • This is a major reason to use Maven or Gradle to manage dependencies for you. – chrylis -cautiouslyoptimistic- Nov 07 '16 at 22:19
  • @bradimus You are correct for both – Ryan Nov 07 '16 at 22:20
  • Try `java -cp BouncyBallV5.0.0A2.jar:joda-time-2.1.jar BouncyBallSource.BBDriver`. For Windows, you might need to make it `java -cp BouncyBallV5.0.0A2.jar;joda-time-2.1.jar BouncyBallSource.BBDriver` (`;` instead of `:`). (Make sure the name of the joda jar is correct -- I don't know which you are using.) – bradimus Nov 07 '16 at 22:30
  • Like @chrylis mentioned, you should look into using Maven or Gradle. – bradimus Nov 07 '16 at 22:32
  • @bradimus I'm trying Maven now, I converted the project to a Maven Project, but I don't understand how to add the jars/how the dependencies work. – Ryan Nov 07 '16 at 23:06
  • @bradimus Turns out I'm really stupid, I couldn't figure out how to get the dependencies to work or any of that, so I just went back to see if there was anything else I could do. Turns out when exporting the project, selecting "Runnable JAR File" does the trick. Thanks for all your help though – Ryan Nov 08 '16 at 01:12

0 Answers0