tags:

views:

72

answers:

2

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\"&gt;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.

A: 

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
pakore
Gave me this error:Exception in thread "main" java.lang.NoClassDefFoundError: WebServiceClient...Could not find the main class: WebServiceClient. Program will exit.
droidy
do you execute it where WebServiceClient.class is? Is your WebServiceClient inside a package?
pakore
Yes. I ran the command in the same directory where WebServiceClient.java and WebServiceClient.class are.
droidy
and that directory contains a lib directory with the jars, right?
pakore
Correct. The directory I'm running the java command contains WebServiceClient.java, WebServiceClient.class, and a folder called lib which contains all of the jars.
droidy
and if you just run "java WebServiceClient" it says that cannot find the libraries but it finds the main function?!?!!?!!?
pakore
Yeah, it's pretty strange. When I just run "java WebServiceClient", I get the original error: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/ws/client/core/WebServiceTemplate
droidy
that's really weird. All you are doing is adding libraries but executing the same class!!
pakore
A: 

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

Andrzej Doyle
Unfortunately that still didn't work.
droidy
The only thing I can think of, then, is that you've got some reasonably complex classloading situation going on, such that the classpath you supply on the command line is **not** the effective classpath of the ClassLoader that's failing to find the file. Just to check - you're writing some kind of web service client, are you sure this error message is being generated on the *client* side? Or is it being generated on the server and returned to the client?
Andrzej Doyle
I changed the command to this (adding a ;. at the end of the jar class paths):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;. WebServiceClientand now its giving me a different error: ServiceClientException in thread "main" java.lang.NoClassDefFoundError: org/springframework/beans/factory/InitializingBeanI think I maybe just need to find the missing JAR.
droidy
Found a couple more jar files I needed and now it's working. Thanks!
droidy