Wednesday, June 26, 2013

How to create web service client using wsdl file(IDE : Eclipse)

To create client stub for wsdl file use following steps.
1.File --> New --> Java project
2.Create web service client(Right click on the above project --> File --> New --> Other --> Web Service Client)
3.Then enter the directory path as follows and press next and finish

file:///C:\Documents and Settings\suresh_inova\Desktop\TestWS_nip\CustomECAwsdl0.0.29\wsdl\CUSTOMECA\CUSTOMECA.wsdl

You can check the created stub for provided wsdl file

Tuesday, June 4, 2013

How to run jar file from command prompt

First go to directory where the jar file is present and execute the following command
java -jar YourSample.jar

If you got following error add MANIFEST.MF file and following entry

Error:Failed to load Main-Class manifest attribute from

Answer : (Enter as Main-Class:package.MainClassName)
manifest-Version: 1.0
Main-Class: com.java.TestSuresh
Ref from: http://stackoverflow.com/questions/2848642/how-to-setup-main-class-in-manifest-file-in-jar-produced-by-netbeans-project

Java Create URL connection

Following is the sample for creating the URL connection when proxy is present

 Test Class

package com.java;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

//import org.apache.commons.codec.binary.Base64;

public class RClient {

      public static void main(String[] args) {
          Map proxyData = new HashMap();
          proxyData.put("pHost", "proxy.site.com");
          proxyData.put("pPort", "8080");
          proxyData.put("pUser", "USERNAME");
          proxyData.put("pPwd", "PWD");

          Map mailData = new HashMap();
          mailData.put("pQuery", "variable1=G&variable2=7120189&variable3=B");
          mailData.put("pUrl", "http://IP/sample/");

          String a = new RClient().sendPostRequest(proxyData, mailData);
      }

      /**
       * @param args :
       */
      public String sendPostRequest(Map proxyData, Map mailData) {
            String data = mailData.get("pQuery");

            try {
                  Authenticator.setDefault(new ProxyAuthenticator(proxyData.get("pUser"), proxyData.get("pPwd")));
                  System.setProperty("http.proxyHost", proxyData.get("pHost"));
                  System.setProperty("http.proxyPort", proxyData.get("pPort"));
                 
                  URL url = new URL(mailData.get("pUrl"));
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  con.setRequestMethod("POST");
                  con.setDoOutput(true);
                  con.setDoInput(true);

                  OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
                 
                  //write parameters
                  writer.write(data);
                  writer.flush();

                  if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {//response code is 200
                        return "OK";
                  } else {
                      return "ERROR: "+ con.getResponseCode();
                  }

            } catch (Exception e) {
                  return "ERROR: "+ e.getMessage();
            }
      }
     
}


================================================
Proxy authenticator class     
     
package com.java;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

class ProxyAuthenticator extends Authenticator {

    private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
}


This site providing you to JAVA Language easy way. I think if you are beginner to JAVA refer this to implements you knowledge using only with very simplest codes.