XML代码运行正常,但junit因NoClassDefFound而失败

时间:2023-02-05 12:13:56

I am upgrading my environment from eclipse 3.3.1 and java 1.4 to eclipse 3.4.1 and java 1.5. My unit tests are in jUnit 3.

我正在将我的环境从eclipse 3.3.1和java 1.4升级到eclipse 3.4.1和java 1.5。我的单元测试在jUnit 3中。

eclipse java version 1.5.0__17

eclipse java version 1.5.0__17

stand alone env version 1.5.0__12, or 1.5.0-17, both work.

独立env版本1.5.0__12,或1.5.0-17,都工作。

I have a method on a class that writes an XML file to disk. It calls TransformerFactory tf = [javax.xml.transform.]TransformerFactory.newInstance(); When I run the code outside of eclipse it runs fine. When I run the code in jUnit in eclipse I get the stack trace below. The missing class is in the rt.jar of java 1.4 and not in java 5, but shouldn't that be abstracted from me?

我有一个类将一个XML文件写入磁盘的方法。它调用TransformerFactory tf = [javax.xml.transform。] TransformerFactory.newInstance();当我在eclipse之外运行代码时运行正常。当我在eclipse中运行jUnit中的代码时,我得到下面的堆栈跟踪。缺少的类是在java 1.4的rt.jar中,而不是在java 5中,但不应该从我这里抽象出来吗?

How can I make the test pass?

我怎样才能通过考试?

I get the same error when I run the code in eclipse from an application.

当我从应用程序中运行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)

update I did some more research in the bowels of the stack trace. The working versions (outside eclipse) are returning an instance of com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl which is the fallback impl class name in javax.xml.transform.TransformerFactory.newInstance()

更新我在堆栈跟踪的内容中做了一些更多的研究。工作版本(在eclipse之外)返回com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl的实例,它是javax.xml.transform.TransformerFactory.newInstance()中的fallback 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());
        }
    }

3 个解决方案

#1


You could check if you are running with the same Java version when running in Eclipse as when running outside of it (in Eclipse: Run As -> Run Configuration... -> JRE tab).

您可以检查在Eclipse中运行时是否使用相同的Java版本运行,以及在其外部运行时(在Eclipse中:运行方式 - >运行配置... - > JRE选项卡)。

#2


I added the following line to setup of the unittest

我添加了以下行来设置unittest

    System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

I figured out what to do with a bit of RTFM. http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()

我想出了如何处理一些RTFM。 http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()

#3


Add the Xalan JAR to the classpath.

将Xalan JAR添加到类路径中。

Also see here if you are using WebLogic. You'll have to put the JAR in shared/lib.

如果您使用的是WebLogic,请参阅此处。您必须将JAR放在shared / lib中。

#1


You could check if you are running with the same Java version when running in Eclipse as when running outside of it (in Eclipse: Run As -> Run Configuration... -> JRE tab).

您可以检查在Eclipse中运行时是否使用相同的Java版本运行,以及在其外部运行时(在Eclipse中:运行方式 - >运行配置... - > JRE选项卡)。

#2


I added the following line to setup of the unittest

我添加了以下行来设置unittest

    System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");

I figured out what to do with a bit of RTFM. http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()

我想出了如何处理一些RTFM。 http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/transform/TransformerFactory.html#newInstance()

#3


Add the Xalan JAR to the classpath.

将Xalan JAR添加到类路径中。

Also see here if you are using WebLogic. You'll have to put the JAR in shared/lib.

如果您使用的是WebLogic,请参阅此处。您必须将JAR放在shared / lib中。