Java NoClassDefFoundError

发布于 2024-09-06 09:52:30 字数 2627 浏览 3 评论 0原文

我可以使用以下命令很好地编译 Java Web Service 客户端:

javac 
     -classpath lib\spring-ws-2.0.0-M2-all.jar;lib\xml-apis.jar;lib\j2ee.jar;lib\saaj.jar;lib\saaj-impl.jar 
     WebServiceClient.java

当我实际运行它 (java WebServiceClient) 时,它会给出以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/ws/client/core/WebServiceTemplate
        at WebServiceClient.<init>(WebServiceClient.java:14)
        at WebServiceClient.main(WebServiceClient.java:37)
Caused by: java.lang.ClassNotFoundException: org.springframework.ws.client.core.
WebServiceTemplate
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 2 more

这是 WebServiceClient.java 的代码:

import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;

public class WebServiceClient {

    private static final String MESSAGE =
        "<message xmlns=\"http://tempuri.org\">Hello Web Service World</message>";

    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    public void setDefaultUri(String defaultUri) {
        webServiceTemplate.setDefaultUri(defaultUri);
    }

    // send to the configured default URI
    public void simpleSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }

    // send to an explicit URI
    public void customSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult("http://wsdl",
            source, result);
    }

    public static void main(String[] args) throws Exception {
        WebServiceClient ws = new WebServiceClient();
        ws.setDefaultUri("http://wsdl");
        ws.simpleSendAndReceive();
    }
}

感谢任何帮助。

I can compile the Java Web Service client fine with the following command:

javac 
     -classpath lib\spring-ws-2.0.0-M2-all.jar;lib\xml-apis.jar;lib\j2ee.jar;lib\saaj.jar;lib\saaj-impl.jar 
     WebServiceClient.java

When I actually run it (java WebServiceClient), it gives me the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/ws/client/core/WebServiceTemplate
        at WebServiceClient.<init>(WebServiceClient.java:14)
        at WebServiceClient.main(WebServiceClient.java:37)
Caused by: java.lang.ClassNotFoundException: org.springframework.ws.client.core.
WebServiceTemplate
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 2 more

Here's the code for WebServiceClient.java:

import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;

public class WebServiceClient {

    private static final String MESSAGE =
        "<message xmlns=\"http://tempuri.org\">Hello Web Service World</message>";

    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    public void setDefaultUri(String defaultUri) {
        webServiceTemplate.setDefaultUri(defaultUri);
    }

    // send to the configured default URI
    public void simpleSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }

    // send to an explicit URI
    public void customSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult("http://wsdl",
            source, result);
    }

    public static void main(String[] args) throws Exception {
        WebServiceClient ws = new WebServiceClient();
        ws.setDefaultUri("http://wsdl");
        ws.simpleSendAndReceive();
    }
}

Any help is appreciated.

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

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

发布评论

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

评论(3

美人骨 2024-09-13 09:52:30

尝试一下

java -classpath lib\spring-ws-2.0.0-M2-all.jar;lib\xml-apis.jar;lib\j2ee.jar;lib\saaj.jar;lib\saaj-impl.jar WebServiceClient

我想你的文件夹结构如下;

\WebServiceClient.java
\WebServiceClient.class
\lib\spring-ws-2.0.0-M2-all.jar
\lib\xml-apis.jar
\lib\j2ee.jar
\lib\saaj.jar
\lib\saaj-impl.jar

Try

java -classpath lib\spring-ws-2.0.0-M2-all.jar;lib\xml-apis.jar;lib\j2ee.jar;lib\saaj.jar;lib\saaj-impl.jar WebServiceClient

I suppose your folder structure is as follows;

\WebServiceClient.java
\WebServiceClient.class
\lib\spring-ws-2.0.0-M2-all.jar
\lib\xml-apis.jar
\lib\j2ee.jar
\lib\saaj.jar
\lib\saaj-impl.jar
时光是把杀猪刀 2024-09-13 09:52:30

当您将该类路径传递给 javac 调用时,这是必要的,因为您的类引用了仅在这些 JAR 中定义的文件。

在运行时也是如此,您编译的 Java 字节码需要能够“查看”这些 JAR,以便加载类并使用 Spring 功能。因此,您不能仅仅调用 java WebServiceClient 并期望它能够工作。

相反,您需要调用 pakore 的答案显示的命令,这看起来应该可以工作。如果有疑问,编译成功后,按向上键重新缓冲最后一条命令,删除 javac 中的 c 并删除 .java文件名在最后。 (如果您的 shell 不支持此功能,请通过记事本等复制并粘贴上一行)。

When you passed in that classpath to your javac invocation, it was necessary because your classes referenced files that were defined only in those JARs.

The same holds true at runtime as well, your compiled Java bytecode will need to be able to "see" those JARs in order to load the classes and use the Spring functionality. So you can't merely invoke java WebServiceClient and expect it to work.

Instead you'll need to invoke the command that pakore's answer shows, which looks like it should work. If in doubt, after successfully compiling, press the Up arrow to rebuffer the last command, delete the c from javac and delete the .java from the filename at the end. (If your shell doesn't support this, copy-and-paste the previous line via e.g. Notepad).

水水月牙 2024-09-13 09:52:30

org.springframework.ws.client.core.WebServiceTemplate 位于 spring-ws-core.jar 上。当应用程序部署到您尝试运行它的应用程序服务器时,您是否检查过它是否包含在 WAR/EAR 中,或者是否包含在服务器库中?成功的编译并不意味着运行应用程序所需的所有类都会在运行时存在。

org.springframework.ws.client.core.WebServiceTemplate is located on spring-ws-core.jar. Have you checked if its included on your WAR/EAR when the application is deployed to the Application Server where you are attempting to run it in or if its included as part of the server's lib? A successful compilation doesn´t mean all classes required to run an application will be there at runtime.

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