Geertjan's Blog

  • August 21, 2005

Yes! Web Service Support in Java Applications!

Geertjan Wielenga
Product Manager
Regular readers of this blog know that I've been eagerly awaiting the availability of web service support in NetBeans J2SE projects. The experiments I've done recently in the web service area have all focused on web applications. Yesterday Milan Kuchtiak, currently the main developer in Prague responsible for web service developments, made a big commit to trunk -- exactly what I had been hoping for for a while now -- web service support in the J2SE area. "Now I'll be able to finish my chess game!" I thought. (Actually, I wasn't the only one who thought that -- Roman and Christopher Atlan both wrote me yesterday, telling me about the J2SE commit and urging me to continue work on the chess game!)

But I was to be disappointed. I started out by trying to consume the simplest web service available on the web:


This web service, discussed earlier in this blog, and also in Consuming Web Services in NetBeans IDE 4.1 (Part 1), is the simplest web service on the web because its operation requires no arguments. All you need to do is invoke an operation on the web service, and then a quotation is returned to the client. So I used the new Web Service Client wizard in the J2SE project. The experience was the same as with web applications -- use the wizard to specify the WSDL URL and the package where the client files will be generated, click Finish and then use the 'Call Web Service Operation' menu item from inside the Java class to generate the code needed to contact the web service. Things were looking good. But then... when I ran the project, I got this java.rmi.RemoteException printed in the IDE's Output window:

java.rmi.RemoteException: HTTP transport error: java.net.UnknownHostException: www.seshakiran.com; nested exception is: 
HTTP transport error: java.net.UnknownHostException: www.seshakiran.com

This was bad news and I didn't know how to solve it. In web applications, a message like this is caused by being behind a firewall and solved by setting the host and port number on the Sun Java System Application Server. But the J2SE project doesn't use a server! So where do you set the host and port number? I was pretty well stuck, until I remembered that chapter 3 ("SOAP with Attachments API for Java") of the J2EE 1.4 Tutorial for NetBeans IDE 4.1 contains several NetBeans-projectized SOAP samples -- including several J2SE samples. I remembered that these samples, somehow or another, made contact with a web service... so if they could do it, so could mine. The sample that I ended up learning a lot from is called MyUddiPing.java. The host and port number are set in a properties file which is passed as an argument (in the Project Properties dialog box). This is the content of my properties file:


The name of the above file is my.properties (but could be called anything with a properties file extension) and that is exactly what I typed in the Arguments field in the project's Run panel (right-click the project in the Projects window to open the Project Properties dialog box and then click the Run panel to find the Arguments field). And this is what I added to the main method of my J2SE project:

 if (args.length != 1) {
System.err.println("Where's your properties file?");
// Retrieve settings from the properties file,
// add to system properties
Properties myprops = new Properties();
myprops.load(new FileInputStream(args[0]));
Properties props = System.getProperties();
Enumeration propNames = myprops.propertyNames();
while (propNames.hasMoreElements()) {
String s = (String) propNames.nextElement();
props.setProperty(s, myprops.getProperty(s));

(By the way, to use the above code, you'll find you need to import java.util.Properties and throw FileNotFoundException and IOException.)

Great, so now everything worked! I was able to contact the quotation web service and invoke its operation -- and here's the evidence:

Next I dug up an e-mail from Gregg Sporar from some time ago. He'd read my blog entry Unjumble That Anagram! and wrote: "Have you by chance looked at the latest dev. build to see how the support for calling web services from a J2SE app is coming? I haven't had time to look at it, but I'd like to add a "Cheat" button to the Anagram app!" That's a great idea -- and so I implemented it. (There were two big problems, though -- under Windows you get an NPE after using the 'Call Web Service Operation' menu item and you have to fiddle around with the project to make it conform to the new web-service style of J2SE projects.) Here's what the Anagram game (you can get the boring non-cheat version from the New Project wizard's Samples/General category) looks like now (the 'guessed word' below was generated when I clicked the 'Cheat' button -- i.e., I didn't type anything, it was all done by the web service):

Apart from adding the host and port number via a properties file, as described above, I turned the J2SE project into a client using the Web Service Client wizard (with this web service), added a Cheat button and this ActionPerformed method (the emboldened line is the invocation on the web service):

private void cheatButtonActionPerformed(java.awt.event.ActionEvent evt) {
String anagram = scrambledWord.getText();
int no_of_letters = anagram.length();
try {
com.toy.anagrams.ui.CountCheatService countCheatService = new com.toy.anagrams.ui.CountCheatService_Impl();
com.toy.anagrams.ui.CountCheatServiceSoap countCheatServiceSoap = countCheatService.getCountCheatServiceSoap();String[] s = countCheatServiceSoap.letterSolutionsMin(anagram,number).getString();
if (s.length > 0) {
for (int i = 0; i < s.length; i++) {
} else {
feedbackLabel.setText("Sorry... word not recognized!");
} catch(javax.xml.rpc.ServiceException ex) {
} catch(java.rmi.RemoteException ex) {

And now... I better start looking at that chess game! I already know what the problem's going to be -- how to turn a move on the chessboard into one of those tricky FEN strings... But, from the perspective of communicating with the web service (once I know what to communicate to it), thanks to the new web service support for J2SE projects, it's going to be a piece of cake!

Join the discussion

Comments ( 2 )
  • Jesper Tuesday, September 6, 2005
    I have just downloadet Netbeans 4.1 and maybe it's me,but how did you activate the Wizard for consuming WS in J2SE projects?
    When i make a J2SE project and R-click the project no WS wizards are visible - a least not like when doing Web App, then I can choose wizards to either creating or consuming WS
  • Geertjan Tuesday, September 6, 2005
    Hi Jesper. You need a post-4.1 Dev build. 4.1 doesn't support it (that's why I was so happy in this blog entry, because only very very recently are all the things described in this blog entry possble). Go here and take a daily 5.0 build. (I'm not sure if it's in q-builds yet.)
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.