classpath – 从命令行运行java程序

使用以下命令编译好我的代码:

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

(lib是我放我的jar文件的地方)

在运行时,我在以下行获得ClassNotFoundException

DatumReader dtmrdr = new GenericDatumReader();

它说它找不到org.apache.avro.generic.GenericDatumReader即使我已经导入了它。

为什么会这样?

谢谢!

导入与加载类或设置CLASSPATH无关。

尝试这个:

 java -cp .;../lib/* Generator 

使用点'.' 因为CLASSPATH中的第一个条目假定Generator.class文件存在于运行java的目录中,并且/lib是该目录的一级。 如果这两个都不正确,请根据需要进行调整。

您应该再次运行程序包括相同的cp:

 java -cp "lib directory where i put all the jars" MainClassOfYourApplication 

用以下代码编译后:

 javac -cp "lib directory where i put all the jars" AvroReader.java 

更多应用于您的示例:

 First step(compile all the needed java files): javac -cp "path/to/jars/*" AvroReader.java //here you should include all the java files not yet compiled but which you need to run your app Second step: java -cp "path/to/jars/*" package.subpackage1.subpackage2.Generator