Compiling and Running the Client Application Sample Ant Build File for a Java Client

6-6 Getting Started With JAX-WS Web Services for Oracle WebLogic Server The example also shows how to invoke an operation that has a user-defined data type examples.webservices.simple_client.BasicStruct as an input parameter and return value. The clientgen Ant task automatically generates the Java code for this user-defined data type. Because the clientgen packageName attribute was set to the same package name as the client application, we are not required to import the clientgen-generated files. package examples.webservices.simple_client; This is a simple Java application that invokes the the echoComplexType operation of the ComplexService Web service. public class Main { public static void mainString[] args { ComplexService test = new ComplexService; ComplexPortType port = test.getComplexPortTypePort; BasicStruct in = new BasicStruct; in.setIntValue999; in.setStringValueHello Struct; BasicStruct result = port.echoComplexTypein; System.out.printlnechoComplexType called. Result: + result.getIntValue + , + result.getStringValue; } } In the preceding example: ■ The following code shows how to create a ComplexPortType stub: ComplexService test = new ComplexService, ComplexPortType port = test.getComplexPortTypePort; The ComplexService class implements the JAX-WS Service interface. The getComplexServicePortTypePort method is used to return an instance of the ComplexPortType stub implementation. ■ The following code shows how to invoke the echoComplexType operation of the ComplexService Web service: BasicStruct result = port.echoComplexTypein; The echoComplexType operation returns the user-defined data type called BasicStruct .

6.2.4 Compiling and Running the Client Application

Add javac tasks to the build-client target in the build.xml file to compile all the Java files both of your client application and those generated by clientgen into class files, as shown by the bold text in the following example: target name=build-client clientgen wsdl=http:{wls.hostname}:{wls.port}complexComplexService?WSDL destDir=clientclasses packageName=examples.webservices.simple_client type=JAXWS javac srcdir=clientclasses destdir=clientclasses Invoking Web Services 6-7 includes=.java javac srcdir=src destdir=clientclasses includes=exampleswebservicessimple_client.java target In the example, the first javac task compiles the Java files in the clientclasses directory that were generated by clientgen, and the second javac task compiles the Java files in the exampleswebservicessimple_client subdirectory of the current directory; where it is assumed your Java client application source is located. In the preceding example, the clientgen-generated Java source files and the resulting compiled classes end up in the same directory clientclasses. Although this might be adequate for prototyping, it is often a best practice to keep source code even generated code in a different directory from the compiled classes. To do this, set the destdir for both javac tasks to a directory different from the srcdir directory. To run the client application, add a run target to the build.xml that includes a call to the java task, as shown below: path id=client.class.path pathelement path=clientclasses pathelement path={java.class.path} path target name=run java fork=true classname=examples.webServices.simple_client.Main failonerror=true classpath refid=client.class.path target The path task adds the clientclasses directory to the CLASSPATH. The run target invokes the Main application, passing it the URL of the deployed Web service as its single argument. See Section 6.2.5, Sample Ant Build File for a Java Client for a full sample build.xml file that contains additional targets from those described in this procedure, such as clean. Rerun the build-client target to regenerate the artifacts and recompile into classes, then execute the run target to invoke the echoStruct operation: prompt ant build-client run You can use the build-client and run targets in the build.xml file to iteratively update, rebuild, and run the Java client application as part of your development process.

6.2.5 Sample Ant Build File for a Java Client

The following example shows a complete build.xml file for generating and compiling a Java client. See Section 6.2.1, Using the clientgen Ant Task To Generate Client Artifacts and Section 6.2.4, Compiling and Running the Client Application for explanations of the sections in bold. project name=webservices-simple_client default=all -- set global properties for this build -- property name=wls.hostname value=localhost property name=wls.port value=7001 6-8 Getting Started With JAX-WS Web Services for Oracle WebLogic Server property name=example-output value=output property name=clientclass-dir value={example-output}clientclass path id=client.class.path pathelement path={clientclass-dir} pathelement path={java.class.path} path taskdef name=clientgen classname=weblogic.wsee.tools.anttasks.ClientGenTask target name=clean delete dir={clientclass-dir} target target name=all depends=clean,build-client,run target name=build-client clientgen wsdl=http:{wls.hostname}:{wls.port}complexComplexService?WSDL destDir={clientclass-dir} packageName=examples.webservices.simple_client type=JAXWS javac srcdir={clientclass-dir} destdir={clientclass-dir} includes=.java javac srcdir=src destdir={clientclass-dir} includes=exampleswebservicessimple_client.java target target name=run java fork=true classname=examples.webservices.simple_client.Main failonerror=true classpath refid=client.class.path java target project

6.3 Invoking a Web Service from a WebLogic Web Service