I have not run a non-jar java program directly from command line for ages, but now that i need it i'm having some problems, the program wont find the needed libs in the class path To reproduce my problem i have created the following, here's the code of the java file:
package launcher;
import org.joda.time.DateMidnight;
public class Launcher {
public static void main(String... args){
DateMidnight mid=new DateMidnight();
System.out.println(mid.dayOfMonth().toInterval());
}
}
in my current directory under my user "on mac", i have only the following jar: joda-time-2.0.jar
and the above .java file. I want to compile it and run it, so i do:
javac -d . -cp joda-time-2.0.jar Launcher.java
I have now in my current folder the following:
Launcher.java joda-time-2.0.jar launcher
Where launcher
is a dir containing Launcher.class
Now if i run java -cp . launcher.Launcher
java -cp . launcher.Launcher
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/DateMidnight
at launcher.Launcher.main(Launcher.java:8)
Caused by: java.lang.ClassNotFoundException: org.joda.time.DateMidnight
i'm telling the vm where the compiled joda library is, whats wrong here?
You included the JAR in the javac
call. But you called java
only with the current directory in the classpath.
Try this:
java -cp .:joda-time-2.0.jar launcher.Launcher
Jars are treated as separate locations/directories, so .
will not represent them. You need to explicitly add them to your classpath while you are compiling and running your code. Use something like
java -cp .;joda-time-2.0.jar launcher.Launcher
or
java -cp .:joda-time-2.0.jar launcher.Launcher
depending on which OS you are using.
You can also use *.jar
wildcard if you have more jars you want to add.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.