Java Object in Actionscript Extension

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
sharingan
Posts: 36
Joined: 26 Oct 2008, 15:40

Java Object in Actionscript Extension

Post by sharingan »

Hi,

I have been reading the documentation chapters 6.6 and 8.7 and am interested in creating my own Java classes in my actionscript extension. However I haven't had much success. Hope someone can help me:

My Java Class code:

Code: Select all

public class ClassName {
	private varType var;

	public ClassName() {
		// some constructor code
	}
	
	public ClassName getClassObject() {
		return this;
	}

	public void functionName(String text) {
		// some code
	}
}
Snippet of my extension code which creates and calls the Java object:

Code: Select all

var pack = Packages.it.gotoandplay.smartfoxserver.data;
var someVar = pack.ClassName.getClassObject();
someVar.functionName(str);
However, when I pass a call to the extension (and I have been calling this extension correctly prior to adding this part of the code), I get an error in the logs:
Error in extension [ extension.as ]: TypeError: getClassObject is not a function, it is org.mozilla.javascript.NativeJavaPackage.
Any idea why I get this error? Also, where should I place this Java class object? Right now, it's in Server\javaExtensions\it\gotoandplay\smartfoxserver\data. Or can I place it anywhere as long as it is in the classpath of the start.bat?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

There is some problem in your code:

Code: Select all

var pack = Packages.it.gotoandplay.smartfoxserver.data;
var someVar = pack.ClassName.getClassObject(); 
You can't call an instance method (getClassObject) on a Class, you have to create an instance first.
(Btw, the getClassObject method is pretty useless, why an object reference should return the same object reference?)

Here's how it should work:

Code: Select all

var pack = Packages.it.gotoandplay.smartfoxserver.data;
var instance = new pack.ClassName(); 
// Test method invocation
instance.functionName("Ciao")
Lapo
--
gotoAndPlay()
...addicted to flash games
sharingan
Posts: 36
Joined: 26 Oct 2008, 15:40

Post by sharingan »

Hi Lapo,

Ok! I managed to fix the error... My own carelessness on stating the wrong package at the top of the Java class file :oops: . Thanks for the help!
Post Reply