Java ME to Java EE Application - Consuming Webservice on Mobile Device

Have you ever wanted to create a mobile client to a webservice? I created a flash demo using NetBeans Mobility Pack for the SDN channel some months ago. I'll describe the flash demo in more details in this blog entry. You can find links to the flash demo, voice transcript and the WSDL of the webservice that I used at the end of this blog entry.


The scenario of the demo was as simple as possible. I wanted to create a mobile application that connect to a webservice and displays the result from the webservice back on the phone. The application had to be able to run on a phone with support for Webservices API (JSR172) but even on a phone without the support for JSR172. The most of devices on market are without the JSR172 today.


I choose a simple public webservice that converts a number into words (e.g. 123 => one hundred twenty three). It's really simple webservice - its argument is a complex type of five strings and a string as return value.


JSR 172 Client

As you can see on the picture bellow the mobile client communicates directly with the webservice. The communication is maintained by JAX-RPC runtime on the device. It works only on devices with the JSR172.

At first I created new mobile application and generated the client to the webservice with New | J2ME Web Service Client wizard.

I needed to specify only the WSDL of the webservice. The wizard generates everything for me. I pressed the Retrive WSDL button and then Finish. It generates stubs and a Sample MIDlet. When you look at the generated Sample MIDlet you can understand how to use the generated stubs. The sample midlet isn't usually "good enough" to be used in a real application. It is useful to check the webservice and to have idea what is it all about.
Note:The sample midlet isn't generated when there are arrays or other types that are not easy to visualize in the webservice.
It was the easier part of the demo... The wizard did everything for me.

Mobile Client to Web Application

Unfortunately this quick solution will not work on a phone that doesn't support Webservices API. NetBeans Mobility Pack will help you to create an application even for these phones. We will have to create a webservices client for the webservice in a web project first. Then we will use this webservice client for generating of a midlet client. The IDE will generate server part and client part for you. The client part will be deployed on your device (phone) and will communicate with the server part that is a middle tier between the mobile client and the webservice. The mobile client will communicate with the webservice client through binary protocol. You don't have to care about the communication at all. It will be generated for you.

Now you know what is the difference between these two solutions.
Let me describe this step in more details. You have to create the middle tier first. Create new webservice client in a web project. Use New | Web Service Client on the web project. Just specify the WSDL of the webservice and let IDE to generate the webservice client. Please note: the webservice client has to be the JAX-RPC one. It's possible to create JAX-RPC clients only in J2EE 1.4 web project. Don't forget to set up the Java EE Version to J2EE 1.4 when you are creating the web project.

When you have the webservice client created you can invoke New | Mobile Client to Web Application wizard that will generate the server and client part of the communication.

Select the web project where is the webservice client, choose a package where the server part will be generated. Use Web Service Client in Web Application radio button and in the combo box select the webservice client. On next panel choose what methods you need from the webservice client. On the last panel specify a name, package of the mobile client part. Let it generate!

Add Preprocessor Blocks + Combine Them Together

The generated code is very similar in both cases (by purpose). I generated both solutions into one directory. I can easily use device fragmentation solution that the Mobility Pack provides and merge the both solution into one source base. I used preprocessor blocks with abilities together with filtering of classes in jar file to achieve this.
I created two configurations in the mobile project. One is with the JSR 172 support and second isn't. I can build distribution jar(jad) for or every configuration separately. I only changed the client used in one of the generated sample MIDlets. It doesn't matter which one you'll choose. The methods for connecting to the webservice are generated the same for both clients. Because of the preprocessor block one client is used in distribution jar for the emulator platform with the JSR172 and the different one for the emulator platform without the JSR172. It is caused by the JSR172 ability in the emulator platform configuration. The code looks like
//#ifdef JSR172
      private ConvertorSoap_Stub client = new ConvertorSoap_Stub();
//#   private WebToMobileClient client = new WebToMobileClient();
That's the only change that I had to do to the client (midlet). When I compile and run the application in different configuration then the right client is used regarding on JSR172 ability in the project properties of the active configuration. The JSR172 ability is created automatically in the configuration if there is Webservices API available or you can add/remove it manually in the project properties.

To be able to compile the application even in the configuration for device platform without JSR172 we have to filter out the classes using JSR172. It can be easily done in project properties.

Then remove the unused MIDlet from the list of the MIDlets in project properties of DefaultConfiguration.

In the demo I changed a little bit the UI of the generated sample MIDlet. I doesn't need to be described here. It is obvious from the demo...


There are some hidden things that you won't see in the demo. You need to set the environment a little bit to be able to create and run the application.
  • Set up your proxies if it's needed
  • It can happen that your the connection fails for JSR172. The server returns "Missing end tag for Body or Envelope". It's WTK bug that can be easily work arounded. Switch the WTK to trusted mode (or use the latest WTK 2.5 where the bug is fixed).



ciao Lukas, thanks for sharing this awesome tutorial, I was trying to do the same but not using SOAP protocol I'm using REST one, that is adopted by yahoo flickr etc for consuming their web services , do you think you can cover that topic as well? thanks in advance Paolo

Posted by Paolo on January 12, 2007 at 01:13 AM CET #

Paolo, i can. I need more detail (I'm lazy to google it). Could you provide me some urls? What yahoo webservice, etc. thanks.

Posted by Lukas on January 12, 2007 at 01:25 AM CET #

sure you can start reading from here As I'm into PHP I can send you this specifics how calling REST services with PHP example for yahoo services and this an example about flickr services

Posted by Paolo on January 12, 2007 at 03:12 AM CET #

Can't create stubs with the Amazon webservice The wsdl works fine while creating a JSE web services client in Netbeans 5.5.

Posted by Milind Rao on January 17, 2007 at 01:51 AM CET #

The Mobility Pack JSR 172 wizard reports that the webservice isn't WS-I compliant. Therefore it will not work with the JSR172.

There is workaround, of course. In this case you should use the "Mobile Client to Web Application".

Posted by Lukas on January 17, 2007 at 05:08 AM CET #

Hi Lukas!
Really nice tutorial!
Although I believe that you should have mention a Netbeans 5.5 Bug.

I couldn't generate the client Stub classes because the path to my project had spaces. After changing the project's path all the code was generated successfully.
unfortunately it took me a day to find that out..
This could be one more Tip in the tutorial.


Posted by Ricardo Santa Ana on January 18, 2007 at 10:27 AM CET #

<img src="" align="right" /> Hi Ricardo,
I tried to generate a jsr172 client to project with spaces. It worked for me - look at the picture on right side. Please report new issue about it with more details -

The JSR172 stub compiler is going to be rewritten therefore it's right time to fix these issues.
Thank you.

Posted by Lukas on January 19, 2007 at 01:15 AM CET #

hmm, the IMG tag doesn't work. Anyway, the link works

Posted by Lukas on January 19, 2007 at 01:17 AM CET #

(third try - it is not a sp\*m) I'm getting a message "Web project must be built before using Mobile Client to Web Application for Web Services" when going through the Web Service Client wizard. It could seem reasonable but I think I already deployed the Web application. The problem is the same as your readers (ilk, kostis) mention at this post: You recommend "The webclient has to be jax-rpc (1.4) in j2ee 1.4 project." But when I am creating web project with Web Service Client wizard I get a warning "To create web service clients in this project, the Java source level must be at least JDK 1.5". I use NB 5.5 with Mobility pack.

Posted by F Jey on February 07, 2007 at 05:51 AM CET #

F Jey, thank you you comments. I received the previous two "spams" as well.
Could you check what Java EE version uses your web project? You will find this information in project properties in Run panel. If your web project is Java EE 5 then it will not work. If it's J2EE 1.4 then it should work.
If it doesn't then try following. The easiest workaround that I know is to try create the web project for the web service client from scratch again. Be sure that the new web project uses Java EE 1.4 (look at Create the web service client again into this new project. Then try to generate Mobile Client to Web with the new webservice client. I hope that it will work.

Let me know...

Posted by Lukas on February 07, 2007 at 07:20 AM CET #

Hi Lukas, thank you for your instant answer. I've tried again - created new web project with Java EE Version J2EE 1.4 and generated Web Service Client. New mobile project still says "Web project must be built before using Mobile Client to Web Application for Web Services" in Mobile Client to Web Application wizard.
Is it possible that there is any magic behind how the web service client is built/deployed? (wizard is complaining about that - "Web project must be built ..." - why?) I right-clicked on the web project and chose Deploy Project before (no problems reported).

Posted by F Jey on February 08, 2007 at 01:00 AM CET #

looks strange... I have no idea :(
Could you report a bug to Issuezilla about it?
Attach your web project there.
Is the webservice that you are using public one? Thanks.

Posted by Lukas on February 08, 2007 at 01:27 AM CET #

Ok, I will report a bug.
At the recent tries I used the web service you use in the demo (
Thanks a lot for your care.

Posted by F Jey on February 08, 2007 at 01:42 AM CET #

Hi Lukas, we're exposing our internal business services as web services (rpc/literal). I guess that "mobile client to web application" approach is the way to go for us. You say "The mobile client will communicate with the webservice client through binary protocol", could you please tell more in details what is happening here? Also, do you have any clever answer to my post here?

Posted by Viggo Navarsete on March 13, 2007 at 02:52 PM CET #

@Viggo: you should read my previous post I'll try to collaborate on the "binary protocol" in my next blog entries.
Unfortunately, I don't have any "clever" answer to your questions at

Posted by Lukas on March 21, 2007 at 02:05 AM CET #

hi lukas, would you happen to have the Convertor.asmx server code? we can't seem to generate the type elements in our wsdl code. we were wondering how you did it based on your Convertor.asmx code. thanks.

Posted by mobile telemedicine on March 26, 2007 at 07:27 AM CEST #

Hi, I have proble while creating J2ME web service client. I have URL for WSDL file "". When I right click the project and select new J2ME web service client it ask me for WSDL URL I specify above URL and when I click on Retrive WSDL I got an error " Download Fail : I/O Exception ". What I do next? Can you suggest me? Thanks

Posted by sap on May 26, 2007 at 03:23 AM CEST #

@sap: the error says more, right? Either "Download failed. I/O exception: Server returned HTTP response code ..." or "Download failed. I/O exception: ... Check the proxy settings".
Check your proxy and connection to internet. If neither of these will work then you can try a workaround to solve it. Download the WSDL locally, and use the "Existing WSDL File" option in the J2ME Web Services wizard.

However it seems that the generator isn't able to process the WSDL because the prefix "soap" for element "soap:binding" is not bound.

Posted by Lukas on May 29, 2007 at 02:52 AM CEST #

Hello Lukas I have a problem when I tried your Demo. After this Step I only got two files the Convertor.wsclient and the Converter.asmx.wsdl but no other classes or sample midlets. I try it several times but every time the same problem. I´m using Netbeans 5.5 with the 5.5 mobility package. Can you help me? Thanks in advance Amel

Posted by Amel on June 23, 2007 at 04:32 PM CEST #

Hello Lukas I am having a problem running the client application on the mobile.I used the 3 tier approach as ma mobile doesn not have the JSR 172 API.The Web Project is buliding fine.But wen i run the client application its sayin that "Communication failed...Please ensure that web project is build and deployed properly after stub creation".Hope u can solve ma problem.

Posted by Sujith on July 12, 2007 at 12:32 AM CEST #

@ Amel: Could you try to check the path to your project? Try to move the project to path without spaces. I remember that we had a problem to figure out what is the cause of this problem. It happened only in some configurations.

@Sujith: the sample projects are at Try to add debugging messages or debug your application to find out why it fails.

Posted by Lukas on July 13, 2007 at 03:10 AM CEST #

Hey guys i am currently working on nokia 6275 which does not support the JSR-172 API.I created a webappication which provides a refernce to the required web service.But wen i test the method of the web service it gives me a Invocation Target Exception.After creating the web app i created the client midlet which wud run on ma mobile device.Here i am gettin the error as i had mentioned above.My problem here is that ma servlet is unable to communicate with the web service.Hoping to see some response.Thank You

Posted by Sujith on July 16, 2007 at 01:35 AM CEST #

@Sujith: it's hard to say what went wrong. Can you reproduce it on emulator? (I need to see the exception stacktrace).

Try that the webservice client is able to communicate with the webservice.

Posted by Lukas on July 16, 2007 at 03:44 AM CEST #

When i test the web service after creating the web application its always givin me Invocation target Exception...why is this happenin all the time???

Posted by Sujith on July 17, 2007 at 12:50 AM CEST #

How can I create a simple Web service like adding two numbers, generate WSDL file and deploy this service?

Posted by Chuong on July 17, 2007 at 04:18 AM CEST #

@Sujith: can you provide me the whole exception stacktrace?

Posted by Lukas on July 17, 2007 at 04:33 AM CEST #


Posted by Lukas on July 17, 2007 at 04:49 AM CEST #

InvocationTargetException org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$ org.openide.util.RequestProcessor$ org.openide.util.RequestProcessor$ Next Exception Layer null sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke( sun.reflect.DelegatingMethodAccessorImpl.invoke( java.lang.reflect.Method.invoke( org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$ org.openide.util.RequestProcessor$ org.openide.util.RequestProcessor$ Next Exception Layer com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl cannot be cast to com.sun.xml.messaging.saaj.soap.MessageImpl com.sun.xml.rpc.client.StubBase._postSendingHook( com.sun.xml.rpc.client.StreamingSender._send( webservice.ServiceSoap_Stub.hello( webservice.ServiceClient.servicesoapHello( sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke( sun.reflect.DelegatingMethodAccessorImpl.invoke( java.lang.reflect.Method.invoke( org.netbeans.modules.websvc.registry.ui.ReflectionHelper.callMethodWithParams( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.invokeMethod( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg.access$500( org.netbeans.modules.websvc.registry.ui.TestWebServiceMethodDlg$ org.openide.util.RequestProcessor$ org.openide.util.RequestProcessor$ I hope this can help u solve ma problem...

Posted by Sujith on July 17, 2007 at 06:56 AM CEST #

@Sujith: it seems that you are running IDE with jdk 6, right? There are two workarounds:
1, run the IDE with jdk 1.5 -
2, or you can download, unzip and put the jars into <nb_install_dir>/java1/modules/ext/jaxrpc16

Thanks to jungi for these tips!

Posted by Lukas on July 17, 2007 at 07:43 AM CEST #

Hey Lukas u have solved one of ma problems i.e i am able to test ma web service.But after i create a client mobile application which make use of the web app and run the app on the emulator it gives me the error sayin that "Communication failed...".Once ma client app connects to the servlet using airtime this error occurs.Lukas,if u cud gimme ur mail-id i cud send u ma applications.Please lemme know if thats fine wit u...Thank You

Posted by Sujith on July 18, 2007 at 01:26 AM CEST #

I am having sun java app server it becaus of that???Do i need to have version 9.0???

Posted by Sujith on July 18, 2007 at 11:00 PM CEST #

Thank you, Lukas,

Posted by Chuong on July 19, 2007 at 02:33 PM CEST #

Thank you Lukas

Posted by Chuong on July 19, 2007 at 02:33 PM CEST #

Hi Lukas,

I tried to build this dilbert sample application provided by Netbeans 5.5.1. I got a similar problem like "F Jey". When trying to create a new mobile client to web application in the mobile application, the wizard does not regognise any webservice it could connect to in the webapplication where i created the webservice client. ("No Web Service Client Available in Project"), together with the warning that i must built the Web project before using the "Mobile Client to Web Application for Web Services".

In the Web application i am using Java EE 1.4, jdk source level 1.5, bundled tomcat 5.5. I also experimented with the provided Diblert sample in NB (it is using wscompile instead of wsimport) but no luck. I really do not know what to do next.

Would very appriciate any pointers, Thanks

Posted by Ben on August 27, 2007 at 07:16 AM CEST #

unfortunately, it doesn't work for the ws client created on tomcat. change it to Glassfish and it will work (at least I hope).
It's connected with the issue

Posted by Lukas on August 28, 2007 at 09:34 AM CEST #

Hi Lukas,
i installed netbeans 5.5.1 and jdk6 update 2, and i want to develop visual web application and in that i need to creat a WebServiceClient for that it is saying to have jdk 1.4 but i installed jdk6 update 2
but it is not showing there...

can u help me in this issue

Thanx in advance

Posted by anitha on September 17, 2007 at 11:39 AM CEST #

You don't need the jdk1.4 but the version of J2EE has to be 1.4 in project properties.

Look at

Posted by Lukas on September 18, 2007 at 08:52 AM CEST #

Hello Lukas,
I first created a Web Service following the steps under Create a Web Service at by choosing "File -> New Project -> Web Application" (I have NetBeans 5.5.1).
But when I type "http://mia:8080/MobileWSApplication/UpdateIncidentWSService?WSDL" in the WSDL url in the new J2ME Web Service Client window, I get the following error and can not continue:
UpdateIncidentWSService.wsdl is not WS-I compliant as required by J2ME Web Service specification.

Am I doing something wrong? There is another way of getting the wsdl? I don't know what to do. Could you guide me?

Thank you!

Posted by Cecilia on September 25, 2007 at 09:49 PM CEST #

Cecilia, could you send me the WSDL + XSD file?<br>
There is lot of restrictions for Java ME WebSrvices API (JSR172) for the WSDL. It might happen that your webservice doesn't fit in those resctrictions.

Posted by Lukas on September 26, 2007 at 03:27 AM CEST #

Hi, Its a great tutorial thanks for it, but please tell me if you have prove this in a real phone, and tell me which,I have a k510a sony ericsson, and it have the api jsr 172, but It does not work with the midlet, can you tell if I need configurate something in the server or the glassfish


Posted by Julian on January 12, 2008 at 07:20 PM CET # new to netbeans and i was following one of the tutorials on creating a j2me client that consumes a web service ( got stuck coz i cudn't find the mobile client to web application there anything else that i need?? i downloaded netbeans 6.1..pliz help

Posted by Moses on August 17, 2008 at 04:02 AM CEST #

@Julian: You probably don't have the Mobility End To End functionality in your IDE - look at

Posted by Lukas Hasik on August 25, 2008 at 06:40 AM CEST #

grr, the last comment was for Moses, of course.

@Julian: I haven't tried this one on real device, I'm sorry. However it should work. Have you set up the internet data connection correctly on your phone? Have you granted access to internet to the application?

Posted by Lukas on August 25, 2008 at 06:46 AM CEST #

Hi Lukas, I have created a webservice as WebService.asmx(using .net)and deployed it into webserver in Desktop.then i have ceated a J2me application in my laptop, after creating Stub using Netbeans6.0,its working fine in my laptop(as it is giving desired output)with default emulator.Now i just deployed it in Nokia 5200(which supports JSR172)by following the "Properties->Deploying->Nokia Terminal connected via PC Suite->Default" and i selected the radio button Deploy to the Selected devices.its giving deployed successful message in output Window(via bluetooth).Now in handset First Form is ok(its displaying an image with welcome note).now as in code i have called waitscreen after clicking ok in 'first form' ,which in turn calls to 'task'which in turn calls to 'webService_Stub',but it's giving error in handset that(no class def found, it could not able to find WebService_Stub(generated stub file)& file).is there any fault during the deployment?.or jad(or jar) file that deployed into nokia5200 includes the hello package (which includes only )and not the webservice package( which includes,Webservice_stub,Webservice.wsclient,WebService.asmx.wsdl).The Webservice_stub object 'ws' invokes a method as ws.Helloworld() method which returns a string "Helloworld".when i commented the ws.helloworld its working wtih no error.but i am getting nothing as i need to invoke Helloworld() method that means there is something to do with ws.Helloworld().any suggestion?? Thanx

Posted by Amit on September 29, 2008 at 07:25 AM CEST #

Hi lukas,i have checked the jad file(by extracting it).It includes all files including WebService_Stub.class & WebService.class(interface class).i am still getting the same error as Application Error: No class Def Found Error java/lang/NoClassDefFoundError java/rmi/RemoteException.I have also tried the same for JSR172Demo avaliable in Netbeans 6.0 it is also running fine within laptop using default emulator but the same error when deployed in nokia 5200.plz help me out. Thanx

Posted by Amit on October 02, 2008 at 05:47 AM CEST #

Hi Lukas,
I have a question, with I'm using netbeans 6.1, the webservice client has to be the JAX-RPC one J2EE 1.4?? It's supported the J2EE5? or can I use any other webservice client and wich one???
pd: sorry for my english \^\^

Posted by Bruno on March 05, 2009 at 11:36 AM CET #

Hi Lukas,
when I run the project PlayRecordsApp, I get communication failed screen after I select a record to play. Can u pls help me with this. I checked everything else and didnt find anything wrong

Posted by yogesh on April 30, 2009 at 11:56 PM CEST #

HI, I follow the steps to generate stub etc, but there is nothing on the emulator screen when I run the project. Dose anyone know why?

Posted by david feng on October 28, 2009 at 11:02 PM CET #

@david feng: Was there a MIDlet generated for you? Or just the stubs?
What is version of your IDE? What is the URL of webservice?

@yogesh: you'll have to debug it (or add debug messages) to see what went wrong during the communication.

Posted by Lukas on October 29, 2009 at 01:39 AM CET #

Thanks for your replies.

Netbean: 6.7.1
I think all stubs are generated,and ConvertedConvertorSoap_StubMIDlet is there. Any ideas?


Posted by david feng on October 29, 2009 at 05:25 PM CET #

Again. here are the error message from debug
C:\\Users\\david\\Documents\\NetBeansProjects\\MobileApplication5\\src\\convertor\\ cannot find symbol
symbol : method get_numberConvertor_ps_InputNo_InputItem()
location: class convertor.ConvertedWebToMobileClientMIDlet
numberConvertor_OutputItem = new StringItem("Number " +get_numberConvertor_ps_InputNo_InputItem().getString()+" converted to words", "<Enter Text>");//GEN-LINE:|34-getter|1|34-postInit
1 error
C:\\Users\\david\\Documents\\NetBeansProjects\\MobileApplication5\\nbproject\\build-impl.xml:368: Compile failed; see the compiler error output for details.

Posted by david feng on October 29, 2009 at 05:28 PM CET #

Hi Lukas,

One intestesting thing is that when I use Netbean 5.5 and Mobility Pack 5 and follow your demo steps again, it looks much better than in Netbean 6.7. But I have the same error 'Comminication failed' posted by Sujith before, so do you have any idea about this? I turned off firewal and protection on my computer.


Posted by david on October 31, 2009 at 09:32 AM CET #

Great post and draw. Thank you for sharing.!!@!

Posted by links of london on November 25, 2009 at 07:29 PM CET #

Hello Lukas,

I'm using netbeans 6.8, and I'm trying to create a web client (web app) that connects to a web service, and generate the code in JAX-RPC style, so I set the Java to JEE 1.4. However, when I try to create the Java ME Client to web app, it says that the project does not have any web service clients. Is there anything I'm missing.

Thank you!

Posted by Juan on February 19, 2010 at 07:26 PM CET #

Hello Lukas. I have the same problem as Juan. Can You give a advise what to do?

Posted by Hide on April 26, 2010 at 06:20 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed

This is the main channel to share news, tips and other stuff related to my work. Sometimes you can find even personal stuff. Enjoy your reading...


« August 2016

No bookmarks in folder