java运行时错误NoClassDefFoundError

时间:2014-06-18 20:49:58

标签: java

人。我有一个关于java运行时链接erorr的问题: 我尝试在centos中编译JasperReport示例代码。

我的设置:

/opt/jasperreports-5.6.0
/opt/apache-ant-1.9.4
/opt/commons-logging-1.1.3/commons-logging-1.1.3.jar

和错误:

~/test/1 localhost $ make run
javac -cp "/opt/jasperreports-5.6.0/dist/jasperreports-5.6.0.jar:." JasperReportCompile.java
java -cp "/opt/commons-logging-1.1.3/commons-logging-1.1.3.jar:"/opt/jasperreports-5.6.0/dist    /jasperreports-5.6.0.jar:."" JasperReportCompile
Compiling Report Design ...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:81)
    at net.sf.jasperreports.engine.JRPropertiesUtil.loadProperties(JRPropertiesUtil.java:99)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.initProperties(DefaultJasperReportsContext.java:94)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<init>(DefaultJasperReportsContext.java:71)
    at net.sf.jasperreports.engine.DefaultJasperReportsContext.<clinit>(DefaultJasperReportsContext.java:59)
    at net.sf.jasperreports.engine.JasperCompileManager.getDefaultInstance(JasperCompileManager.java:197)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:539)
    at JasperReportCompile.main(JasperReportCompile.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 8 more
make: *** [run] Error 1

我没有设置全局CLASSPATH // Makefile

JAVAC := javac
JAVA  := java
JAVA_COMPILE_LIBS = "/opt/jasperreports-5.6.0/dist/jasperreports-5.6.0.jar:."
JAVA_RUNTIME_LIBS = "/opt/commons-logging-1.1.3/commons.logging-1.1.3.jar:${JAVA_COMPILE_LIBS}"
TARGET := JasperReportCompile

.PHONY: clean all run

all: ${TARGET}

${TARGET}: ${TARGET}.java

%:%.java
    ${JAVAC} -cp ${JAVA_COMPILE_LIBS} $<

run: ${TARGET}
    $(JAVA) -cp ${JAVA_RUNTIME_LIBS} $<

clean:
    rm -rf *.class

// JasperReportCompile.java

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;

public class JasperReportCompile {

   public static void main(String[] args) {
      String sourceFileName = "~/test/1/a.xml";
      System.out.println("Compiling Report Design ...");
      try {
         JasperCompileManager.compileReportToFile(sourceFileName);
      } catch (JRException e) {
         e.printStackTrace();
      }
      System.out.println("Done compiling!!! ...");
   }
}

我不明白为什么在将apache commons jar添加到类路径后仍然会出现此错误。希望有人可以帮助我。非常感谢。

1 个答案:

答案 0 :(得分:3)

我猜文件名commons.logging-1.1.3.jar是错误的。它必须是commons-logging-1.1.3.jar。(不是Point)