GUIDE: Using Java 8 with SFS2X

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

GUIDE: Using Java 8 with SFS2X

Postby Lapo » 03 Jul 2015, 14:57

SmartFoxServer 2.10 (and higher) supports Java 8 but it is still provided with a Java 7 runtime.

For those who are interested in coding server-side Extension in Java 8 you can follow these simple steps to replace the JRE in your SFS2X installation.

1) Download the JRE 8 from Oracle's website:
http://www.oracle.com/technetwork/java/ ... 33155.html
Making sure to match the architecture of your OS (32 vs 64 bit)

2) Uncompress the download and replace the new jre/ folder under the root of your SmartFoxServer 2X installation. This will overwrite the previous JRE.
(This step is not required for MacOS X)

3) One last step is required for the embedded Jetty server:
Open the SFS2X/lib/jetty/modules/npn/ folder and duplicate one of the files in the folder. Example: duplicate file "npn-1.7.0_45.mod" and rename it so that the version number matches the new JRE.
Example if you have downloaded JRE 1.8.0_25 the file will be renames as --> "npn-1.8.0_25.mod"

You're ready to run SFS2X with Java 8
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: GUIDE: Using Java 8 with SFS2X

Postby Flying_Banana » 11 Jul 2015, 10:42

I am running SFS on Mac and I can't find a jre folder anywhere...

I had no problem finding it in the Linux installation though.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 11 Jul 2015, 12:18

You're right, the Mac is an exception because it's the only OS that comes with a pre-installed JDK.
In order to run SFS2X with Java 8 under OSX you just need to install the JDK8 on your Mac. You can skip step #2.

I've added a note in the main post.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: GUIDE: Using Java 8 with SFS2X

Postby Flying_Banana » 11 Jul 2015, 12:40

I've recently installed Java 8, and in terminal Java -version gives java 1.8.0_u45. However when I tried to run the server with the new package built with eclipse in 1.8 compiler, I got a version problem, which disappeared when I disabled the extension that is built in 1.8.

Is there a way to check the Java version that SFS is using?


Edit: somehow it fixed itself! Not exactly sure what I did...

Commenting on Java 8 though, the feature I think is the most important is the new Compute method from the lambda calculus addition of Map interface. It allows me to modify ConcurrentHashMap without having to worry about locking the data structure, which is crucial to multithreaded environment like SFS!
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 11 Jul 2015, 18:48

Actually the ConcurrentHashMap already handles concurrency even without Java 8.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: GUIDE: Using Java 8 with SFS2X

Postby Flying_Banana » 13 Jul 2015, 08:31

Yes, but it is only concurrent for read and write operations. I cannot, for example, read a value, increment it and write it back atomically.

However, with Java 8, compute method is guaranteed to run an entire function atomically.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 13 Jul 2015, 09:00

Flying_Banana wrote:Yes, but it is only concurrent for read and write operations. I cannot, for example, read a value, increment it and write it back atomically.

However, with Java 8, compute method is guaranteed to run an entire function atomically.

Ah yes, indeed.
Out of curiosity is it really guaranteed to run atomically? The official doc doesn't seem to say that.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: GUIDE: Using Java 8 with SFS2X

Postby Flying_Banana » 13 Jul 2015, 10:56

Yep! It isn't actually stated in the updated interface, but in the compute function under ConcurrentHashMap it says:

Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). The entire method invocation is performed atomically. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this Map.


The benefit is immense.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 13 Jul 2015, 12:44

Cool :)
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: GUIDE: Using Java 8 with SFS2X

Postby Flying_Banana » 22 Jul 2015, 22:39

I'm still having some troubles. It seems like occasionally Smart Fox will dislike Java 8 and use Java 7 on restart.

So: working Java 8 extension running...I modify the JAR, replace it, restart server, then found out server didn't start. Check log, it says class loader error: Unsupported major.minor version 52.0. This is the signature error that indicates Java 8 code is being loaded by JRE 7. But I was running on Java 8 extension code before restart!

Code: Select all

java -version

returns Java 8. This is quite annoying...

(...and now it's working again, after deleting, recompiling, cleaning projects several times)

Image
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 23 Jul 2015, 06:34

There seem to be some JRE conflict on your system.
If your Extension is compiled for Java8 you will necessarily need to replace the SFS2X embedded JRE with a new JRE8.

Have you done this? If not follow these simple steps:
http://smartfoxserver.com/blog/?p=281

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
stickman
Posts: 9
Joined: 30 Aug 2008, 12:48

Re: GUIDE: Using Java 8 with SFS2X

Postby stickman » 16 Sep 2015, 04:42

I'm experiencing the same thing. "Unsupported major.minor version 52.0". Extension is compiled with 1.8.0_60 and java -version returns:

Code: Select all

java version "1.8.0_60"


Plus,

Code: Select all

ls -alh /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
returns

Code: Select all

/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/


So I know the system is running 1.8. I added the .mod file as well, but I can't imagine that's related to this issue.

Any other way to debug this?

Thanks.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 16 Sep 2015, 06:52

SmartFoxServer does not run with your system's Java Runtime, it uses its own, which is still Java 7.
What is happening is that you have compiled your server code in Java 8 and you're attempting to run it in JVM 7, thus causing the error.

All you need to do is replace the SFS2X embedded JRE with the latest JRE 8, by following the 3 steps outlined in the opening post of this thread.

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
stickman
Posts: 9
Joined: 30 Aug 2008, 12:48

Re: GUIDE: Using Java 8 with SFS2X

Postby stickman » 16 Sep 2015, 11:36

I had followed the instructions, however #2 should be split into 2 steps or re-written for clarity :)

2) Uncompress the download and replace the jre/ folder under the root of your SmartFoxServer 2X installation, replacing the existing folder. This will overwrite the previous JRE. For MacOSX users the jre/ folder will be new to the directory.

When you say "This step is not required for MacOS X" I logically assumed you were referring to the entire step #2.

Even with this, the SFS2X.app fails to load properly and throws the same error. Running the /sfs2x.sh script loads the new JRE since it looks for it and works, but this method cripples the OSX .app.

EDIT: To get the SFS2X.app working, I right clicked it and selected "Show Package Contents". From there, I opened up /Content/Resources/script and changed the following to replace the jre.bundle with the new JRE placed as instructed above:

Code: Select all

#JRE7="../.install4j/jre.bundle/Contents/Home/jre/bin/java"
JRE8="../jre/bin/java"
${JRE8} -cp  "./:lib/*:lib/jetty/lib/*:extensions/__lib__/*" -Dfile.encoding=UTF-8  com.smartfoxserver.v2.Main $1 $2


Works as expected now.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GUIDE: Using Java 8 with SFS2X

Postby Lapo » 16 Sep 2015, 14:22

Thanks.
Point #2 is indeed to be skipped entirely under Mac OS X, because the Oracle Java installer will perform a global installation, system-wide.

The easiest way to get up and running is simply to install Java 8 and then launch SFS2X via sfs2x.sh script in the terminal. There actually a total of 4 ways to launch SFS2X on the Mac :( Three of which depend on the embedded JRE and require further hacking into the shell scripts.

We plan to release the next update embedding Java 8 by default, avoiding any other manual changes.

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 46 guests