Expand my Community achievements bar.

Cannot Import Java Classes

Avatar

Level 1
I am trying to do the sample contact walkthrough in FLEX
builder. For the Java Adapter.




http://livedocs.macromedia.com/flex/2/docs/wwhelp/wwhimpl/js/html/wwhelp.htm?href=00000138.html



I cannot get any import statements to work for the java class
files. I have tried this using the FDS as a J2EE application
running in tomcat, I have tried with the standalone JRUN version of
the FDS app.



I have put the contact files everywhere I can think of, in
the server classes folder, in the project, in the default web
folder and nothing I try works. I have even setup the project to
compile on the server and I get nothing. Everytime, I get errors.



I did the tutorial from the JDJ magazine:


http://java.sys-con.com/read/210991.htm




I cant get that java connection to work either. Please
someone help me.



The flex code is below.



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:Script>

<![CDATA[



import mx.data.DataService;

import mx.collections.ArrayCollection;

import samples.contact.Contact;



public var ds:DataService;

[Bindable]

public var contacts:ArrayCollection;

//public var contact:Contact;

]]>

</mx:Script>



<mx:DataGrid id="dg" editable="true">

<mx:columns>

<mx:DataGridColumn dataField="contactId" headerText="Id"
editable="false"/>

<mx:DataGridColumn dataField="firstName"
headerText="First Name"/>

<mx:DataGridColumn dataField="lastName" headerText="Last
Name"/>

</mx:columns>

</mx:DataGrid>

</mx:Application>
8 Replies

Avatar

Level 1
OK, trying to do the remote object thing, I have setup a
destination in my remoting-config.xml file. Which says this:



<destination id="SimpleJava">

<properties>

<source>javaSamples.SimpleMath</source>

</properties>

</destination>



I know at this point that I am accessing that destination,
because if I mess up the flex reference to it, I get unknow
destination. But now using this destination I get this client
error.



<destination id="SimpleJava">

<properties>

<source>javaSamples.SimpleMath</source>

</properties>

</destination>



I am no longer trying to import the java classes directly,
but using the remot object aliasing.



I placed the actual java class file in the webinf
directory.

Avatar

Level 3
The error message does not look correct, or at least what an
error message should look like.



Let's check a couple of things.



1) You have a compiled class called SimpleMath in your
WEB-INF/classes/javaSamples directory.

2) You have mapped the class in your remoting-config.xml
file. If what you have above is in that file, it should be OK as
long as you have your adapter and channel set, something like



<adapters>

<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true"/>

</adapters>



<default-channels>

<channel ref="my-amf"/>

</default-channels>



3) Your remote object is referencing SimpleJava as it's
destination.



If you could send your entire code sample, I may be able to
help more.

Avatar

Level 1
Ken, thank you for replying, I have had absolutly nothing but
problems trying to get this 'simple' thing to work.



Here is my code.



In the remote-config.xml file, I have this code:

<?xml version="1.0" encoding="UTF-8"?>

<service id="remoting-service"

class="flex.messaging.services.RemotingService"

messageTypes="flex.messaging.messages.RemotingMessage">



<adapters>

<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true"/>

</adapters>



<default-channels>

<channel ref="my-amf"/>

</default-channels>



<destination id="SimpleJava">

<properties>

<source>javaSamples.SimpleMath</source>

</properties>

</destination>

</service>



Here is my MXML file: (Called new.MXML)

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:RemoteObject id="math" destination="SimpleJava">

<mx:method name="doMath" concurrency="last"
result="fillTxt(event.result as int)"/>

</mx:RemoteObject>

<mx:Script>

<![CDATA[



import mx.rpc.events.ResultEvent

private function fillTxt(b: int):void{

txtResult.text = (String)
;

}

//comment

]]>

</mx:Script>

<mx:Panel x="10" y="24" width="250" height="200"
layout="absolute" title="Simple Math with Java">

<mx:Label x="10" y="10" text="Input Number to
Multiply:"/>

<mx:TextInput x="10" y="36" id="txtNumber"
maxChars="1"/>

<mx:Label x="10" y="126" id="txtResult"/>

<mx:Label x="10" y="100" text="Your Number will appear
below:"/>

<mx:Button x="10" y="66" label="Multiply by 2"
click="math.doMath(txtNumber.text)"/>

</mx:Panel>



</mx:Application>





Here is my SimpleJava.java file:



package javaSamples;

//import java.util.*;

/**

* @author jsanders

*

*/

public class SimpleMath {

public int doMath(String a){

int new_a = Integer.parseInt(a.trim());

int b = new_a * 2;

return b;

//changesd

}



}





Like I said I have had absolutly nothing but problems with
this. Yesterday, I was getting send failures, So I did some
reasearch found some folks who said that they had the same problem,
and they had to hard code the app name into the services-config.xml
file. The file used to have {context.root} in it, and that variable
was set to /flex, now I hardcoded this line into the my-amf channel
definition to get rid of the send failure error.



<channel-definition id="my-amf"
class="mx.messaging.channels.AMFChannel">

<endpoint uri="
http://{server.name}:{server.port}/flex/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>

<properties>

<polling-enabled>false</polling-enabled>

</properties>

</channel-definition>



As you can see what should say:


http://{server.name}:{server.port}/{context.root}



Now says:


http://{server.name}:{server.port}/flex.



This was the only way I could get the send error to go away.
Now I worry that I might have broken something else. Since I
removed all references to {context.root} in that file.



{context.root} is defined in the flex-config.xml file, like
this:

<context-root>/flex</context-root>



I have tried it with or without the '/' in the value of the
context.root variable.



I have played with the remoting-config file, and I know that
I am accessing the destination.



I have played with the <source> option of the
destination, and I know that I am finding the class and making a
good connection.



I am using the JRUN install of FDS on my box, and yesterday
at the end of the day after getting the context.root situation
sorted out, I got a different error something like major.minor
versioning errors.



So the Java guys here at work told me that the JRUN version
packaged with FDS was 1.4, and I used 1.5 to compile my java class.
So I recompiled this morning with the 1.4 sdk. Now I dont get that
error about the major minor versioning anymore.



Now I get nothing. I use firefox, and all the browser tells
me is 'transferring data from localhost' Nothing else. It just sits
there and does nothing.



When I did the debug on it, I got into a loop on the
UIComponent.as file in the callLaterDispatcher(event:Event)
function.



Any clues what might be causing this new hang up. Basically
the machine just hangs. Is there a problem with the result from the
Method I am calling in the MXML? Did I do something wrong with the
Event as Int thing?



Again any help would be appreciated.









Avatar

Level 1
O and one other thing. Yes my SimpleJava folder is under the
webInf/classes directory. Inside that folder is the
SimpleMath.class file.

Avatar

Level 3
Hi there,



I'd recommend setting the server log level in your
services-config.xml file to "Debug" (search for the <logging>
section).



Also, make sure this pattern is listed in the the
<filters> section: <pattern>Message.*</pattern>



This will log out all inbound and outbound messages to the
console, so you'll want to start your app server at the command
line. Alternately, you can use the ServletLotTarget class which
will log to the general app server log.



Once you have this logging enabled you'll be able to see
whether the message is received by the server, any exceptions that
occur during processing as well as the response returned to the
client.



Best,

Seth

Avatar

Level 3
The problem is that you are compiling locally and then
running on the server. I prefer this approach. The problem is that
the reference are resolved for some reason - don't know why, Adobe
can't seem to give me an answer that makes any sense to me.



My work around was to set the endpoint of the remote object
to "/flex/messagebroker/amf/"



Once I did that all worked for me.

Avatar

Level 3
Hi Ken,



The value to swap into the {context.root} token, unlike the
server name and port (which can be discovered on the client at
runtime), is exposed by a J2EE/Servlet container when a request is
being processed; see HttpServletRequest.getContextPath().



For swfs that are compiled outside of a servlet container you
should use the -compiler.context-root option when you invoke mxmlc
in order to provide the actual value that should be used for this
token. Or just hardcode the correct context root in your channel
definition endpoint URIs in the configuration file as you've done
:)



Best,

Seth



Avatar

Level 3
Yeah -



I think you and I have had this discussion before. I have
never been able to get either of those two options to actually
work.



Ken