By
In this article we will check how .NET and Java can talk to each other using web services. Again I will assume that the reader of this article is not in a position to afford high-end tools that involve significant financial commitments. I will also assume that the reader of this article uses Windows 2000 or Windows XP Professional as operating system. So what do we need for the start? From .NET side we will need .NET Platform SDK and ASP.NET Web Matrix. Maybe the best place to get those is www.asp.net, there you will find all the information about download and installation process. If you are using dial-up (like me) then paying a visit to local Microsoft and exploring the possibility to get .NET Platform SDK on CD is a good idea. If you happen to be in possession of VS.NET then you already have .NET Platform SDK and you can use VS.NET instead of ASP.NET Web Matrix. From Java side the list is longer but download is altogether smaller. First we need JavaTM 2 SDK, Standard Edition, that could be downloaded from http://java.sun.com/j2se/1.4/, when we are at Sun Microsystems web site we may download also JavaTM Web Services Developer Pack, the link is here http://java.sun.com/webservices/downloads/webservicespack.html. WSDP contains Tomcat and lot of other goodies which we are going to need later to make Axis feel at home on our machine. The story about visiting local Sun branch and exploring the possibility of getting those two on CD is certainly an option here. Finally, we will need Axis and for its WSDL tools crimson. For those two we will look at http://xml.apache.org/axis and http://xml.apache.org/crimson/. At the moment of writing this article Axis 1.1 RC2 is the latest available binary download. If you are still in the mood for more downloads there is a number of free IDE for Java which you may wish to try, like :
- NetBeans IDE 3.5 http://www.netbeans.org/
- Eclipse 2.x http://www.eclipse.org/
- jEdit 4.x http://www.jedit.org
to name a few. Free IDE for .NET which is very good is SharpDevelop and it could be found here http://www.icsharpcode.com/. If you had enough of downloading, don't worry, simple text editor and command line approach is something you can always rely on.
Installing all that on Windows
So you got .NET Platform SDK, webmatrix.msi, jwsdp-1_1-windows-i586.exe (or later), j2sdk-1_4_1-win.exe (or later), axis-1_1rc2.zip and crimson-1.1.3-src.zip (or bin). All that comes in form of exe or msi file is associated with wizard which will guide you through installation process. Accept defaults and consistently choose Next until you are done. Prior to installation of .NET Platform SDK be sure that you have IIS installed. In our exercise we won't use IIS at all, so when you are done with installation you can shut it down. If you encounter any difficulty during installation of any of those feel free to ask Microsoft or Sun for advice, they have FAQ-s and forums dedicated to their products where answers to most commonly encountered problems can be found. Now we may check how Tomcat and ASP.NET Web Matrix WebServer.exe work. Don't try them together because both of them are using http://localhost:8080 by default. To start WebServer.exe we could open ASP.NET Web Matrix IDE, create some hello world web page and press F5. It will show Start Web Application dialog where you can pick parameters like directory or port number. If you are happy with suggested parameters select Start button and WebServer.exe will start with work. To move WebServer.exe to a different port execute from command line WebServer /port:9090 /path:"C:\Inetpub\wwwroot\testws2" /vpath="/testws2", if that is where your preferred virtual directory is and port 9090 is port which you would like to use. Also, you can configure it from ASP.NET Web Matrix using GUI when you press F5. To start Tomcat we need to execute startup.bat which is typically in C:\Documents and Settings\Administrator\jwsdp-1.1\bin directory if you are logged in as Administrator and you are using WSDP 1.1. If you are planning to spend some time with Tomcat you will find out that C:\Documents and Settings\Administrator\jwsdp-1.1 is also called CATALINA_HOME. Give some time to Tomcat to start and point browser to http://localhost:8080. If there is index page Tomcat is up and running. To move Tomcat to another port open C:\Documents and Settings\<How you are known to your computer>\jwsdp-1.1\conf\server.xml in text editor and search and replace 8080 with desired port value. Be sure that all 8080 entries are replaced with new value. That is under condition that you are using WSDP 1.1, if you are using another WSDP version change jwsdp-1.1 to jwsdp-1_0, jwsdp-1.2 or whatever it may be in your case.
Now we need to install Axis. While building crimson is not too difficult (and Ant is included in WSDP) I would advise to get Axis as binary distribution. Unpack axis-1_1rc2.zip somewhere, for example into D:\axis-1_1rc2. There are two main ways how Axis could be installed on Tomcat. The first is : locate D:\axis-1_1rc2\webapps\axis and copy it to C:\Documents and Settings\Administrator\jwsdp-1.1\ webapps. Start Tomcat and point browser to http://localhost:8080/axis, you should see Axis welcome page. The other way is to edit C:\Documents and Settings\<How you are known to your computer>\jwsdp-1.1\conf\server.xml and create Context for Axis. The simplest is to locate admin context and to looking at it create axis context, something like this :
<Context className="org.apache.catalina.core.StandardContext"
crossContext="false" reloadable="false"
mapperClass="org.apache.catalina.core.StandardContextMapper"
useNaming="true" debug="0" privileged="true"
wrapperClass="org.apache.catalina.core.StandardWrapper"
docBase="D:/axis-1_1rc2/webapps/axis" cookies="true" path="/axis"
cachingAllowed="true"
charsetMapperClass="org.apache.catalina.util.CharsetMapper">
<Logger className="org.apache.catalina.logger.FileLogger"
debug="0" verbosity="1" prefix="localhost_axis_log."
directory="logs" timestamp="true" suffix=".txt"/>
</Context>
So instead of copying axis directory we tell Tomcat to look for it. To test it point browser to http://localhost:8080/axis. What about crimson? Axis WSDL tools will need it so place crimson.jar in lib directory. This concludes installation and now we can do some work.
Writing and deploying simple services
We will now create two similar services to test cross-platform interoperability. In .NET, or to be more precise in C# we will do this :
<%@ WebService language="C#" class="InteropHW" %>
using System;
using System.Web.Services;
using System.Xml.Serialization;
public class InteropHW {
[WebMethod]
public int ArrLength(int[] a) {
return a.Length;
}
}
To deploy it we need to save it in some virtual directory using asmx extension. If we are using ASP.NET Web Matrix IDE it will do that for us. In Java we will do something similar :
public class JInteropHW {
public String[] getArr(int l) {
return new String[l];
}
}
To deploy it we will save it in webapps\axis directory as JInteropHW.jws. Some of us will now take time and try to compare WSDL documents describing those two services. They will discover quite a few differences and also similarities between them. If we start studying WSDL now this article will last forever, so lets go to cross-platform clients.
Using Web Services
From web services point of view it is not important where the client or service is or in which programming language it is written and we will exercise that approach. To create .NET client we will use WSDL.EXE. After we are sure that Tomcat is running we will execute from command line wsdl http://localhost:8080/axis/JInteropHW.jws?WSDL. WSDL.EXE will do what we expect from it (create proxy) and results of its work will be saved in file named JInteropHWService.cs. We will be curious and open it to see what is inside. First we will see a kind of warning which says between other things :
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
So we must do only the changes which will cause correct behavior and we mustn't keep regenerating code. To repair generated proxy we will locate constructor :
public JInteropHWService() {
this.Url = "http://localhost/axis/JInteropHW.jws";
}
WSDL.EXE made some adjustments to URL of our web service and if we want to ever get in touch with it we need to add port number. I like to add Main to proxy, less typing, and it looks like this :
static void Main(){
JInteropHWService jhw = new JInteropHWService();
Console.WriteLine(jhw.getArr(9).Length);
}
So we can save it, compile it and finally execute it to see how that works. We will find out that it works without problems.
Now is the time to switch sides. We will shut down Tomcat and start WebServer.exe. To create proxy we will use org.apache.axis.wsdl.WSDL2Java. First we need to place all the necessary libraries in CLASSPATH. Maybe the best way is to create one batch file with this content :
set AXIS_HOME=D:\axis-1_1rc2
set JWSDP_HOME=C:\Documents and Settings\Administrator\jwsdp-1.1
set CLASSPATH=.;%AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-ant.jar;%AXIS_HOME%\lib\commons-discovery.jar;
%AXIS_HOME%\lib\commons-logging.jar;%AXIS_HOME%\lib\jaxrpc.jar;%AXIS_HOME%\lib\saaj.jar;%AXIS_HOME%\lib\crims
on.jar;%AXIS_HOME%\lib\wsdl4j.jar;%AXIS_HOME%\lib\log4j-1.2.4.jar;%JWSDP_HOME%\jaxp-1.2.2\lib\endorsed\dom.ja
r;%JWSDP_HOME%\jaxp-1.2.2\lib\endorsed\sax.jar;%JWSDP_HOME%\jaxp-1.2.2\lib\jaxp-api.jar
If you are logged in as Administrator and you are using WSDP 1.1 it will work out of box, if you are logged under a different name or you are using different WSDP, please adjust it to your environment. When all the required libraries are found and placed in CLASSPATH we will execute from command line java org.apache.axis.wsdl.WSDL2Java http://localhost:9090/InteropHW.asmx?WSDL. In the directory where we opened command line window there will be newly created folder org\tempuri containing five new files. Take your time and see what is inside. We will see that org.apache.axis.wsdl.WSDL2Java also did a bit of renaming. In the directory where we opened command line we will create client. This is the code :
public class InteropHWClient
{
public static void main(String [] args) {
try {
org.tempuri.InteropHW il = new org.tempuri.InteropHWLocator();
org.tempuri.InteropHWSoap i = il.getInteropHWSoap();
org.tempuri.ArrayOfInt ip = new org.tempuri.ArrayOfInt();
ip.set_int(new int[5]);
System.out.println("Length of array is " + i.arrLength(ip));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Save it as InteropHWClient.java and compile using javac InteropHWClient.java. If CLASSPATH is not set there will be lot of errors, to set CLASSPATH execute that batch file prior to compilation and if necessary execution. Finally we can execute our client using java InteropHWClient. Everything will work as expected.
Conclusion
Maybe .NET and Java are fierce competitors, but that doesn't mean that they can't talk to each other. We just had the opportunity to see how that may look like. If you are C# programmer oriented towards Web Services you just can't afford to ignore what Java part of the world does in the same area. The same is valid if you are Java programmer, you must be informed what .NET has to offer in Web Services field. Otherwise you are reducing your chances to get new contracts and you are missing half the fun. If you think that learning is not fun, then ask yourself do you really want to be a programmer.
About myself
Currently I'm writing for CodeNotes. http://www.codenotes.com and preparing presentation for International Web Services Conference & Exposition in Toronto, Canada on October 14-16, 2003. For more info visit http://www.WowGao.com. If you have any suggestions, questions or lucrative job offers my address is filipbulovic@mail.com, I will try to answer.