Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

JAX-WS Stand Alone Web Services Using JDK1.6 Wsimport Utility Client

In this tutorial we will learn, how to create JAX-WS client by wsimport utility:

Requirements:
1. Java 1.6+
2. Eclipse
3. Windows OS or any.
4. You have already followed my first example for writing the service calss and all from click or video MathService using wsgen utility

Video: 




Open the eclipse
Create the new project as java project.
File > New > Java Project.

Enter the project name: Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client
Java project will created in with src and JRE System Library
Note: assuming you have publish the earlier project Math Service and you will able to access the wisdl by any browser, URL > http://localhost:8888/webservice/www.tutorialbyexample.com?wsdl 
and getting the wsdl as response.

if not then follow the previous project first, Click me

Now open the command prompt.
Change the dir: cd C:\workspace\Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client 

and type the command:
C:\workspace\Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client>wsimport -s src -keep -d class http://localhost:8888/webservice/www.t
utorialbyexample.com?wsdl

Press enter, now your client supporting code will generated, like wise below screen.



Write the client code:

Right click on the src under project Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client.

Click New > Class.
Enter the package name:com.tutorialbyexample.mathservice.client
Enter the Class name:MathServiceClient

MathServiceClient.java code will look like:

packagecom.tutorialbyexample.mathservice.client;

importcom.tutorialbyexample.mathservice.MathService;
importcom.tutorialbyexample.mathservice.MathServiceService;

public class MathServiceClient {

       public MathServiceClient() {
              // TODO Auto-generated constructor stub
       }

       public static void main(String[] args) {
              //
              MathServiceService objMathServiceService = newMathServiceService();
              MathService objMathService = objMathServiceService.getMathServicePort();
              System.out.println(objMathService.add(10, 40));

       }

}

Hole project directory and file will look like:



Run the MathServiceClient client.
Right click on the MathServiceClient > Run as > Java Application.

Output will appear on the console:
50.0


All done form my end, try at your end. Put your point of view if any.

Have a good day ahead!!

Video link: MathService using wsgen

You can watch this video from Click



JAX Web Services end to end examples (Server & Client code, Tomcat & Eclipse)


Requirements:
JDK 1.6 or 1.6+
Eclipse
Tomcat 6

JAX-WS-JAVA-WEB-SERVICES













Let’s start one by one step to achieve it:


Step : Create a Dynamic Web project in Eclipse
Open the eclipse by double click on the eclipse – Shortcut
File > New > Dynamic Web project
Project Name: jaxwsprojectineclipse
Target runtime: Apache Tomcat v6.0
Dynamic web module version: 2.5
Click Next
Click Next
Select or check: Generate web.xml deployment descriptor
Click on the Finish.

Step : Create the GreetingWebService.java service interface
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select Interface
Name : GreetingWebService
Package : com.tutorialbyexample.webservicejaxws
Click on the Finish

Copy and pest below code:

GreetingWebService.java

package com.tutorialbyexample.webservicejaxws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
* @author Vinod Kumar Web Service endpoint interface
*/

@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface GreetingWebService {

                /**
                 * sayWelcome with parameter as string
                 */
                @WebMethod
                String sayWelcome(@WebParam(name = "sName") String sName);
}

Step : Create the GreetingWebServiceImpl.java service interface
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select class
Name : GreetingWebServiceImpl
Package : com.tutorialbyexample.webservicejaxws
Click on the Finish

Copy and pest below code:

GreetingWebServiceImpl.java

package com.tutorialbyexample.webservicejaxws;

import javax.jws.WebService;

import com.tutorialbyexample.webservicejaxws.GreetingWebService;

/**
*
 * @author Vinod Kumar This is service implementation class
*/

@WebService(endpointInterface = "com.tutorialbyexample.webservicejaxws.GreetingWebService")
public class GreetingWebServiceImpl implements GreetingWebService {

                @Override
                public String sayWelcome(String sName) {
                                return "Welcome " + sName + " !";
                }
}

Step : Add/update the deployment descriptor sun-jaxws.xml and web.xml
Right click on the WebContent folder located under jaxwsprojectineclipse
Select New
Others
Type XML
Select XML File
Next
Type the file name: sun-jaxws.xml
Click on the Finish

Copy and pest sun-jaxws.xml:
<?xml version="1.0"encoding="UTF-8"?>
       version="2.0">
       <endpoint name="GreetingWebService"
              implementation="com.tutorialbyexample.webservicejaxws.GreetingWebServiceImpl"
              url-pattern="/GreetingWebService"/>
</endpoints>

Update/Add the require line in web.xml

<?xml version="1.0"encoding="UTF-8"?>
  <display-name>jaxwsprojectineclipse</display-name>
  <listener>
    <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
  </listener>
  <servlet>
    <servlet-name>GreetingWebService</servlet-name>
    <servlet-class>
              com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GreetingWebService</servlet-name>
    <url-pattern>/GreetingWebService</url-pattern>
  </servlet-mapping>
</web-app>

Step : Write the WebServicesClient.jav client code
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select class
Name : WebServicesClient
Package : com.tutorialbyexample.webservicejaxws.client
Click on the Finish

Copy and pest below code:

WebServicesClient.java
package com.tutorialbyexample.webservicejaxws.client;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.tutorialbyexample.webservicejaxws.GreetingWebService;

/**
*
 * @author Vinod Kumar This is service implementation class
*/

public class WebServicesClient {

                public static void main(String[] args) throws MalformedURLException {
                                URL url = new URL(
                                                                "http://localhost:8080/jaxwsprojectineclipse/GreetingWebService?wsdl");

                                // argument first, refer from wsdl
                                // argument second, refer from wsdl
                                QName qname = new QName(
                                                                "http://webservicejaxws.tutorialbyexample.com/",
                                                                "GreetingWebServiceImplService");

                                Service service = Service.create(url, qname);

                                GreetingWebService hello = service.getPort(GreetingWebService.class);
                                System.out.println(hello.sayWelcome("Vinod Kumar"));
                }
}


Step : Write the client as jsp file WSClientJSP.jsp
Right click on the WebContent folder located under jaxwsprojectineclipse
Select New
Others
Type JSP
Select JSP file
Next
Type the file name: WSClientJSP.jsp
Click on the Finish

Copy and pest below code WSClientJSP.jsp:
<%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<%@ page
       import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,com.tutorialbyexample.webservicejaxws.GreetingWebService"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

       <%
              URL url = new URL(
                           "http://localhost:8080/jaxwsprojectineclipse/GreetingWebService?wsdl");

              // argument first, refer from wsdl
              // argument second, refer from wsdl
              QName qname = new QName("http://webservicejaxws.tutorialbyexample.com/",
                           "GreetingWebServiceImplService");

              Service service = Service.create(url, qname);

              GreetingWebService hello = service
                           .getPort(GreetingWebService.class);
              out.println(hello.sayWelcome("Vinod Kumar"));
       %>
</body>
</html>


Step: Deploy war file jaxwsprojectineclipse .war in Tomcat
Right click on the jaxwsprojectineclipse
Select Export
Select WAR File
Let it be Web Project Name : jaxwsprojectineclipse
Select or Type the Destination: C:\Tomcate\apache-tomcat-6.0.39\webapps\jaxwsprojectineclipse.war
Note: This is the location of current tomcat installation directory.
Select or check
Export Source files
Overwrite existing file
Click the Finish
jaxwsprojectineclipse.war war file will create in Tomcat C:\Tomcate\apache-tomcat-6.0.39\webapps\

Step: Start the Tomcat
Open the Tomcat installation directory C:\Tomcate\apache-tomcat-6.0.39\bin
Double click on the startup.bat file

Tomcat will start:
INFO: Starting Coyote HTTP/1.1 on http-8080
org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/13  config=null
org.apache.catalina.startup.Catalina start
INFO: Server startup in 1441 ms

Step: Verify the deploy web services by WSDL file
Open the any web browser and type the end point url

Output:
  <?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.10 svn-revision#919b322c92f13ad085a933e8dd6dd35d4947364b. 
  -->
- <!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.10 svn-revision#919b322c92f13ad085a933e8dd6dd35d4947364b. 
  -->
- <types>
- <xsd:schema>
  </xsd:schema>
  </types>
- <message name="sayWelcome">
  <partname="parameters" element="tns:sayWelcome" />
  </message>
- <message name="sayWelcomeResponse">
  <partname="parameters" element="tns:sayWelcomeResponse" />
  </message>
- <portType name="GreetingWebService">
- <operation name="sayWelcome">
  <outputwsam:Action="http://webservicejaxws.tutorialbyexample.com/GreetingWebService/sayWelcomeResponse" message="tns:sayWelcomeResponse" />
  </operation>
  </portType>
- <binding name="GreetingWebServiceImplPortBinding" type="tns:GreetingWebService">
  <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="sayWelcome">
  <soap:operationsoapAction="" />
- <input>
  <soap:bodyuse="literal" />
  </input>
- <output>
  <soap:bodyuse="literal" />
  </output>
  </operation>
  </binding>
- <service name="GreetingWebServiceImplService">
- <port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
  </port>
  </service>
  </definitions>

Step: Verify the deploy services with WSClientJSP.jsp

Output:
Welcome Vinod Kumar !

Step: Verify the deploy web services by DII client
Right click on the WebServicesClient.java file located under jaxwsprojectineclipse > Java Resource
Select Run As
Select Java Application
Output will there in Console
com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.tutorialbyexample.webservicejaxws.jaxws.SayWelcome
com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.tutorialbyexample.webservicejaxws.jaxws.SayWelcomeResponse
Welcome Vinod Kumar !

Please like and share it!!!

How to create executable jar in Java by Eclipse?

How to create executable jar in Java?



Following are the step for creating executable jar file in java:
Step: Software environments:
JDK
Eclipse

Step: crate java project in Eclipse
Open the Eclipse
File > New > Java Project
Enter the:
Project name: Jar-Creation-In-Java-TutorialByExample
Select the JRE
JavaSE-1.7
Click on the
Next
Finish.


Step: Add ExecutableJarCreationInJava.java in Jar-Creation-In-Java-TutorialByExample
Right click on the src folder under Jar-Creation-In-Java-TutorialByExample
Click on the
New
Class
Enter the package name: com.tutorialbyexample
Enter the Name: ExecutableJarCreationInJava

Click on the Finish.

Step: ExecutableJarCreationInJava.java

package com.tutorialbyexample;

/*
* Main for for Executable Jar Creation In Java process
*/
public class ExecutableJarCreationInJava {

public void getDetails(Object object) {

printLn(object);

}

/*
* private method for print info on console
*/
private void printLn(Object object) {
System.out.println(object);
}

}

Step: Add MainApp.java in Jar-Creation-In-Java-TutorialByExample
Right click on the src folder under Jar-Creation-In-Java-TutorialByExample
Click on the
New
Class
Enter the package name: com.tutorialbyexample
Enter the Name: MainApp
Click on the Finish.

Step: MainApp.java

package com.tutorialbyexample;

/*
* Main for for Executable Jar Creation In Java process
*/
public class MainApp {

/*
* main method for Main App
*/
public static void main(String[] args) {
ExecutableJarCreationInJava objJarInJava = 
                     new ExecutableJarCreationInJava();
objJarInJava
                .getDetails("Jar file creation done in Java by Eclipse!!!");
}

}

Step: Run it in Eclipse for simple output
Right click on the MainApp.java
Run As
Java Application
Console: Jar file creation done in Java by Eclipse!!!

Step: Create the executable jar file mainapp.jar in Eclipse
Right click on Jar-Creation-In-Java-TutorialByExample
Click on the Export
Type the Jar and Click on the Runnable JAR file from wizard
Click on the Next
Select project name under Lunch Configuration: Jar-Creation-In-Java-TutorialByExample
Export destination: C:\mainapp.jar
Let it be rest default setting
Click Finish.

mainapp.jar will create in C:\mainapp.jar.

Step: Directory structure for mainapp.jar
If you have 7-zip, just right click on the mainapp.jar and extract it.

mainaap
--com
-- tutorialbyexample
-- ExecutableJarCreationInJava.class
-- MainApp.class
--META-INF
-- MANIFEST.MF


Step: MANIFEST.MF file details
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.tutorialbyexample.MainApp

Note: With help of this only jar file converted into executable.

Step: Run the executable jar mainapp.jar form command line.
Open the command prompt
Start > Type cmd and press Enter
Command prompt window will appear
Change the dir to c:\ as our mainapp.jar file are located under this dir.

Type the command java and enter

c:\>java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp
-classpath
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D=
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:...|:]
-enableassertions[:...|:]
enable assertions with specified granularity
-da[:...|:]
-disableassertions[:...|:]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:[=]
load native agent library , e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:[=]
load native agent library by full pathname
-javaagent:[=]
load Java programming language agent, see java.lang.instrument
-splash:
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.

Indicate that java home has been set if not then set the java home.

Output:

c:\>java -jar mainapp.jar MainApp

Jar file creation done in Java by Eclipse!!!

Thanks for reading, please like and share!!!

Reference:
Java 1.7 by Oracle
Eclipse

Video:


Web Services Java Client Tutorial DII


Creating the Web Services Client, Dynamic Invocation Interface (DII), If project is already then then just add the one class which is have one method for invoking the web services call. Or just add the java project in eclipse and add the class.

Open the eclipse and click on the File
New
Project
Java Project
Next
Enter the Project Name: webservicesclient
Choose the JRE version 1.5+
Click on the next
Next
Finished

Now java project with webservicesclient has been created.

Add the java client class:

Right click on the src of webservicesclient project.
New
Class
Name: JavaWebServiceClient
Package name: com.webservicesclient.client
Finished.

Blank java file will created like below:

JavaWebServiceClient.java

package com.webservicesclient.client;
public class JavaWebServiceClient {
}