Run SmartFoxServer 2X with JRE 16 and higher

As of 2023 SmartFoxServer 2X ships with the JRE 11 (LTS) out of the box, but it can also run with a higher version if needed.

In our documentation we have a detailed table with all the supported Java releases and their level of compatibility with SFS2X, which we recommend to consult.

At the time of writing this article SFS2X does not run with a JRE version 16 or higher without additional configuration. In this short article we will discuss how to configure the SmartFoxServer launchers to support the most recent Java runtimes, up to the latest JRE 20.

» Farewell Javascript

Before we discuss the changes to the launcher we need to reiterare one important limitation when replacing the default JRE. Since JDK 15 the Nashorn Javascript engine has been removed and therefore it’s not possible to use Javascript Extensions starting at that release and higher.

This is essentially the only limitation we have found running SFS2X with the latest JDKs and, with this out of the way, we can take a look at how to setup the SFS2X in order to proceed.

» Replacing the JRE

To replace the default JRE with a new one you can follow these simple steps:

  • Download the JRE matching your OS and architecture from your chosen vendor
  • Stop SmartFoxServer 2X, if it is running
  • Replace the default jre/ folder found under the main SmartFoxServer_2X/ directory with the new one (keeping the same name)

Before launching SFS2X we need to add a number of JVM settings to the launcher. SmartFoxServer 2X provides essentially two different launchers:

  • The sfs2x.sh / sfs2x.bat for local testing and development
  • The sfs2x-service for running the server as a service in staging or production environments

This is what sfs2x.sh should look like, where the MODULE_FIX variable contains the new settings required by the most recent JDKs:

JRE_FOLDER="../jre/bin"
JAVA_CMD="java"
if [ -d "$JRE_FOLDER" ]; then
    JAVA_CMD="../jre/bin/java"
fi

CPATH="./:lib/*:lib/apache-tomcat/bin/*:extensions/__lib__/*"

MODULE_FIX="--add-opens=java.base/java.util=ALL-UNNAMED
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED
--add-opens=java.base/java.text=ALL-UNNAMED
--add-opens=java.desktop/java.awt.font=ALL-UNNAMED
--add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"

${JAVA_CMD} -cp ${CPATH} -Dfile.encoding=UTF-8 ${MODULE_FIX} com.smartfoxserver.v2.Main $1 $2

Under Windows the sfs2x.bat should look like this, with all parameters on one line:

@..\jre\bin\java.exe -cp "./;lib/*;lib/apache-tomcat/bin/*;extensions/__lib__/*" -Dfile.encoding=UTF-8 --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED com.smartfoxserver.v2.Main %1 %2

Save the file and that’s all: you can now start SmartFoxServer 2X.

» Modifying the sfs2x-service launcher

To modify the service launcher we need to append the additional parameters to the sfs2x-service.vmoptions file, one item per line:

-classpath/p ./:lib/*:lib/apache-tomcat/bin/*:extensions/__lib__/*
-Dfile.encoding=UTF-8
--add-opens=java.base/java.util=ALL-UNNAMED
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED
--add-opens=java.base/java.text=ALL-UNNAMED
--add-opens=java.desktop/java.awt.font=ALL-UNNAMED
--add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED
--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED

Also the launcher performs a JDK version check to verify the mininum and maximum version compatiblity. At the moment the max version allowed is 16 and we need to change it:

  • Open the sfs2x-service file with a text editor
  • Search for “16” in the file

You will find a line like that looks like this:

  if [ "$ver_major" -gt "16" ]; then

Replace 16 with a higher value, such as 50, just to skip the check.

Save and we’re done. Now you can launch the SmartFoxServer 2X service with sfs2x-service start.

» F.A.Q.

Q: What is the advantage of running SFS2X with the latest JDK?

There isn’t any specific advantage other than using some of the latest language features that are not present in previous releases.

Q: Are there any performance benefits in running the latest JDK?

Not that we have seen. We’ve load-tested SFS2X running on JRE 19 vs JRE 11 and didn’t find signifcant differences, so we don’t recommend switching purely for performance reasons.

Q: What about the addition of virtual threads in Java 19/20?

Virtual threads (a.k.a. “Project Loom”) are very interesting for scalability, especially when dealing with thousands of requests that may wait on slow I/O, such as Extensions calling a database, external services and similar. However this feature is still in preview stage and we wouldn’t recommend it for a busy production server. The feature should be finalized in JDK 21.

Q: Is there any difference in developing Extensions for the latest JDKs that one shoule be aware of?

Not really. You can follow our documentation on how to build your own Extensions and it will work for any supported JDK (at the time of writing it is between JDK 11 and 20).

Just keep in mind that some of the features in the latest JDKs are still in preview mode and as such may note be entirely stable. Also, features in preview mode require an extra JVM switch to be enabled (typically it is: –enable-preview). We recommend checking the relative javadoc for further details.