Java NoClassDefFoundError
我可以使用以下命令很好地编译 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下
我想你的文件夹结构如下;
Try
I suppose your folder structure is as follows;
当您将该类路径传递给
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
fromjavac
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).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.