classpath - running a java program from the command line

Viewed 37390

My code compiled fine with the following command:

javac -cp "../lib/*" AvroReader.java

(lib is where i put my jar files)

At run time I get a ClassNotFoundException on the following line:

DatumReader<?> dtmrdr = new GenericDatumReader();

It says it can't find org.apache.avro.generic.GenericDatumReader even though I've imported it.

Why is this happening?

3 Answers

To compile and execute java file on Linux System with external jar files :

javac -cp jar_file1.jar:jar_file2:jar_file3.jar:. java_program_name.java

java -cp new_mail_api.jar:activation.jar:additional.jar:.java_program_name

To compile and execute java file on Windows System with external jar files :

javac -cp jar_file1.jar;jar_file2;jar_file3.jar;. java_program_name.java

java -cp new_mail_api.jar;activation.jar;additional.jar;.java_program_name

In Unix of Linux, Java Classpath contains names of the directory with colon “:” separated, On Windows Java Classpath will be semicolon “;” separated while if you defined java classpath in Manifest file those will be space separated.

For more knowledge about Classpath, visit https://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html

Related