我正在将环境从 eclipse 3.3.1 和 java 1.4 升级到 eclipse 3.4.1 和 java 1.5。 我的单元测试在 jUnit 3 中。
eclipse java 版本 1.5.0__17
独立环境版本 1.5.0__12 或 1.5.0-17,都可以工作。
我有一个类方法,可以将 XML 文件写入磁盘。 它调用 TransformerFactory tf = [javax.xml.transform.]TransformerFactory.newInstance(); 当我在 eclipse 之外运行代码时,它运行良好。 当我在 Eclipse 中的 jUnit 中运行代码时,我得到下面的堆栈跟踪。 缺少的类在 java 1.4 的 rt.jar 中,而不是在 java 5 中,但这不应该是从我这里抽象出来的吗?
怎样才能让测试通过?
当我从应用程序在 Eclipse 中运行代码时,出现同样的错误。
java.lang.NoClassDefFoundError: org/apache/xalan/processor/TransformerFactoryImpl at weblogic.xml.jaxp.RegistryTransformerFactory.(RegistryTransformerFactory.java:62) at weblogic.xml.jaxp.RegistrySAXTransformerFactory.(RegistrySAXTransformerFactory.java:12) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at java.lang.Class.newInstance0(Class.java:350) at java.lang.Class.newInstance(Class.java:303) at javax.xml.transform.FactoryFinder.newInstance(FactoryFinder.java:100) at javax.xml.transform.FactoryFinder.findJarServiceProvider(FactoryFinder.java:278) at javax.xml.transform.FactoryFinder.find(FactoryFinder.java:185) at javax.xml.transform.TransformerFactory.newInstance(TransformerFactory.java:103) at com.bellsouth.snt.cnmp.sso.netcool.NetcoolAccessThread.writeXmlFile(NetcoolAccessThread.java:278) at com.bellsouth.snt.cnmp.sso.netcool.NetcoolAccessThreadTest.testWriteXmlFile(NetcoolAccessThreadTest.java:83) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at junit.framework.TestCase.runTest(TestCase.java:164) at junit.framework.TestCase.runBare(TestCase.java:130) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
更新 我对堆栈跟踪的内部做了一些更多的研究。工作版本(在 Eclipse 之外)返回 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl 的实例,它是 javax.xml.transform.TransformerFactory.newInstance() 中的后备 impl 类名
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError { try { return (TransformerFactory) FactoryFinder.find( /* The default property name according to the JAXP spec */ "javax.xml.transform.TransformerFactory", /* The fallback implementation class name, XSLTC */ "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); } catch (FactoryFinder.ConfigurationError e) { throw new TransformerFactoryConfigurationError( e.getException(), e.getMessage()); } }
最佳答案
我在单元测试的设置中添加了以下行
System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
我想出了如何使用一点 RTFM。 http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()
关于java - XML 代码运行正常,但 junit 失败并出现 NoClassDefFounderror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/521692/