0

I'd like to generate dates sequence in a range. This thread suggests to use Joda-Time package. I downloaded it and unzipped it to the same directory as my Main.java.

When I try import ./joda-time-2.7/org.joda.time.DateTime;, the compiler says:

Main.java:4: error: expected

import ./joda-time-2.7/org.joda.time.DateTime;

 ^

Main.java:4: error: expected

import ./joda-time-2.7/org.joda.time.DateTime;

  ^

Main.java:4: error: class, interface, or enum expected

import ./joda-time-2.7/org.joda.time.DateTime;

    ^

And when I try import org.joda.time.DateTime;, the compiler says:

MissingDateSearch.java:5: error: package org.joda.time does not exist

import org.joda.time.DateTime;

              ^

It seems that I didn't include the package into my building path. This thread discusses how to set the building path for java. Currently, my "environment" file only has PATH variable. I don't want to make any global change just for one project.

So my question is, does any one know how to include the package in a way without global change? Or does any one has a simple way to generate dates in a range without joda-time?

Thanks!

ADDED

This problem can be bypassed by using IDEs like eclipse. Any terminal based solutions are still welcomed.

Community
  • 1
  • 1
user3813057
  • 891
  • 3
  • 13
  • 31

1 Answers1

0

Use the

import org.joda.time.DateTime;

import statement and ensure that JodaTime jar file is on your classpath at compile & runtime

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thanks! However, when I try `javac Main.java -classpath ./joda-time-2.7/ `, it says "error: package org.joda.time does not exist". – user3813057 Feb 10 '15 at 23:15
  • you need to add the JAR file to your compile classpath, e.g. `javac -cp .:joda-time-1.6.2.jar Main.java` – Reimeus Feb 10 '15 at 23:17
  • When I try `javac Main.java -cp ./joda-time-2.7/joda-time-2.7.jar`, it compiles fine. However, when I run the program, it has "Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant". I run the same code as the accepted answer in [this post](http://stackoverflow.com/questions/8166390/java-generate-all-dates-between-x-and-y). – user3813057 Feb 10 '15 at 23:32
  • Use the same classpath when running the application – Reimeus Feb 10 '15 at 23:35
  • Thanks for your instruction, but `java Main -cp ./joda-time-2.7/joda-time-2.7.jar` still results in the same exception as `java Main`. – user3813057 Feb 10 '15 at 23:39
  • You're missing the current directory from the classpath as I have shown above – Reimeus Feb 10 '15 at 23:49
  • I'm not sure if I understand your answer correctly. I used `javac Main.java -cp ./joda-time-2.7/joda-time.2.7.jar` since the jar file is under the "joda-time-2.7" directory and has the same name. This complies fine. When I run the program using `java Main -cp ./joda-time-2.7/joda-time-2.7.jar`, it has the above exception, which is the same exception when I run the program using `java Main`. I do not understand the symbol ".:" in your first answer, but I think I've already included in the class path by the -cp option. But I still get the exception. – user3813057 Feb 11 '15 at 01:07