XML 代码运行正常,但 junit 失败并显示 NoClassDefFound

发布于 2024-07-13 11:07:26 字数 3915 浏览 5 评论 0原文

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

java 版本 1.5.0__17

独立 env 版本 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());
        }
    }

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 java version 1.5.0__17

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

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?

How can I make the test pass?

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

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()

    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());
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深白境迁sunset 2024-07-20 11:07:33

将 Xalan JAR 添加到类路径。

如果您使用的是 WebLogic,另请参阅此处。 您必须将 JAR 放入共享/lib 中。

Add the Xalan JAR to the classpath.

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

还不是爱你 2024-07-20 11:07:32

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

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).

懒的傷心 2024-07-20 11:07:31

我在单元测试的设置中添加了以下行,

    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()

I added the following line to setup of the 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文