[Guide] Java 1.4 development for first-timers

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

Moderators: Lapo, Bax

User avatar
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

[Guide] Java 1.4 development for first-timers

Postby BigFIsh » 05 May 2010, 21:09

As most of you would probably know by now, SFS2X server side script will be based on Java 6 only. (yay, good bye old AS1).

So you may want to consider converting your AS1 code to Java 1.4 now to prepare for Java 6 to come.

Here's a simple guide to get you started with Java development for server side extension:

Step 1: If you haven't done already, download JDK (link) and a java IDE (software to develop java). I downloaded the JDK + NetBeans bundle.

Note: Thiss guide is based on NetBeans 5.5 IDE.

Step 2: Using the java extension documentation, follow the guides to get you started. Don't worry about "Deploying the extension" just yet.

Step 3: Once your project is set up, go to "Files" tab and expand build.xml. Open "-post-compile" and add the following content within the tags:

Code: Select all

<copy todir="{Your SFS installation path}\Server\javaExtensions">
   <fileset dir="${build.dir}/classes/"/>

What this does, is that after "building" your .java file (F11 in NetBeans) - it will automatically copy the .class file and place it inside the javaExtension directory. SmartFoxServer is already set up to look for extension files inside that folder. A .class file is used for java extension for SFS, NOT the .java file itself. .java is just solely for development purposes.

(Thanks to Godai for this method - as it have saved me lots of time)

Step 4: Attach extension to your Room or Zone in your config.xml file. It'll be something like this:

Code: Select all

      <extension name="extId" className="yourClassName" type="java" />

Don't include ".class" in the className parameter.

Step 5: Well, that was the easy part. Now it's time to get you started with java development! The good news is that if you know AS and/or php very well, learning java should be piece of cake.

Start off by having a look at somes .java examples inside your javaExtensions\it\gotoandplay\extensions\examples, particularly SimpleExtension.java (to start with). Your new .java file should be based on SimpleExtension's template (so just copy and paste, obviously).

Then you can progress further by looking at PixelGame.java and SFSTris2.java. You could probably start coding by now.

This SFS Java Cookbook also provides great examples for most common things.

The SFS java documentation is also a handy reference.

Step 6: NetBeans comes with a in-build debugger. Follow this excellent tutorial created by Godai to get you started. It allows you to create breakpoints for your extension.

With NetBeans 5.5, there is a known bug where the "local variables" (and watchers) type and value column has invisible values. A newer version of NetBeans 5.5 fixed this bug. I'm using NetBeans 6.8 and it's fixed in that version.

Things you may need to know:
1. ExtensionHelper (java) is like _server (AS) except that it no longer contains sendResponse function. i.e. ExtensionHelper.sendRoomList()

2. "this." refers your class which extends AbstractExtension, and contains the sendResponse function. i.e. this.sendResponse(..)

3. If there's more than one function with the same name, Java automatically recognises the correct one based on the parameters being taken.

4. org.json.* package is a nightmare. (Requires you to try and catch lots of time). Use net.sf.json.* package instead for JSON. It's more up to date.

5. When creating a new String[], Object[], Integer[] etc variable, you need to define its memory allocation. i.e. String[] myStringList = new String[5]; (this will only hold 5 string instances in the array).

6. Java doesn't have Array.join() function by default, but StringUtils does. So you'll need to download the apache common package from here and import StringUtils into your .java file like this: import org.apache.commons.lang.StringUtils.

7. If you're getting these warnings: "[unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map" when attempting to "put" a new variable for properties, i.e. user.properties.put("varName", var) or room.properties.put("varName", var) then use @SuppressWarnings("unchecked") at the top of the function to ignore those warnings.

8. To get more detailed warnings after "compiling", add the following parameter "-Xlint:unchecked" via your Project properties > Build > Compiling (in NetBeans).

9. See this thread regarding the use of net.sf.json.* and org.json.* library. At the moment, SFS can only receive org.json.JSONObject. In that thread, Nebulous provided a good snippet for converting org.json.JSONObject into net.sf.json.JSONObject. Note: don't import both org.json.* and net.sf.json.* libraries otherwise you'll get ambigous errors.

10. Good luck!
Smartfox's forum is my daily newspaper.
Posts: 28
Joined: 19 Sep 2010, 16:32

Postby mephi » 20 Nov 2010, 13:26

Great info BigFIsh!
Thanks! :)

2 questions:
- Why not make this post sticky?

- What is the Java way of doing _server.logger.warning() ?
User avatar
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 20 Nov 2010, 19:25

1. This post was created to the purpose of preparing users for SFS2x. As SFS2x is already out, this post is no longer useful as they would either: a) start writing Java 6 from scratch or b) convert from AS1 to Java 6.


Code: Select all

import it.gotoandplay.smartfoxserver.SmartFoxServer;
SmartFoxServer.log.warning("Uh oh..");
Smartfox's forum is my daily newspaper.
Posts: 28
Joined: 19 Sep 2010, 16:32

Postby mephi » 20 Nov 2010, 21:29

Thank you!

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 12 guests