[Solved] Error NoClassDefFoundError for interface

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

Moderators: Lapo, Bax

MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

[Solved] Error NoClassDefFoundError for interface

Postby MicroEyes » 25 Feb 2019, 16:36

Hi,
I have a main zone extension jar "myZoneExtension.jar". I have a room extension jar "myRoomExtension.jar". I have a "common.jar" which contains an interface say ITraceable. I intend to use ITraceable interface in both my zone and room extension so I have placed my "common.jar" in /extensions/MyGame/_lib_ folder. So my final folder hierarchy is:

/extensions/MyGame/
/extensions/MyGame/myZoneExtension.jar
/extensions/MyGame/_lib_/common.jar
/extensions/MyGame/_lib_/myRoomExtension.jar

ITraceable interface:

Code: Select all

package game.common;
public interface ITraceable {
    public void trace(Object... a_args);
}


In zone jar, i have below class structure:

Code: Select all

publc class BaseServerEventListener_C implements ISFSEventListener, ITraceable {
   @Override
   public void trace(string a_msg) {
      ...doing something here.
   }
}

public class GameHandler extends BaseServerEventListener_C {
...
}

public class myZoneExtensionextends SFSExtension {
    @Override
    public void init() {
        GameHandler m_gameCreatorRef = new GameHandler();                           <-------------- CRASHING HERE.
        addEventListener(SFSEventType.SERVER_READY, m_gameCreatorRef);
    }
}



Issue: When starting server i am getting NoClassDefFoundError error:

Code: Select all

java.lang.NoClassDefFoundError: game/common/ITraceable
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at game.zone.myZoneExtension.init(myZoneExtension.java:54)
        at com.smartfoxserver.v2.entities.managers.SFSExtensionManager.createExtension(SFSExtensionManager.java:303)
        at com.smartfoxserver.v2.entities.managers.SFSZoneManager.createZone(SFSZoneManager.java:426)
        at com.smartfoxserver.v2.entities.managers.SFSZoneManager.initializeZones(SFSZoneManager.java:239)
        at com.smartfoxserver.v2.SmartFoxServer.start(SmartFoxServer.java:297)
        at com.smartfoxserver.v2.Main.main(Main.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.exe4j.runtime.LauncherEngine.launch(Unknown Source)
        at com.exe4j.runtime.WinLauncher.main(Unknown Source)
        at com.install4j.runtime.launcher.WinLauncher.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: game/common.ITraceable
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)



Can you please tell me what am I doing wrong?
I read the classloader concept and put the common jar in _lib_ folder to share ITraceable interface across zone and all room extensions.
Last edited by MicroEyes on 26 Feb 2019, 17:09, edited 1 time in total.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Error NoClassDefFoundError for interface

Postby Lapo » 26 Feb 2019, 08:03

Hi,
the problem is that the _lib_ folder is located directly under SFS2X/extensions/. The one you have created in your own Extension folder will be ignored.

The files layout should look like this:

Code: Select all

extensions/_lib_/common.jar
extensions/MyGame/myZoneExtension.jar
extensions/MyGame/myRoomExtension.jar


You could also move the Room Extension to the _lib_ folder, if you prefer not to create multiple copies of the same classes for each created Room. It saves memory.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: Error NoClassDefFoundError for interface

Postby MicroEyes » 26 Feb 2019, 11:18

Hi Lapo,
I'll try this. BUT If i keep common.jar outside my extension folder then the jar will be visible and accessible to my other project's extensions too. That's why i preferred to keep inside my extension folder. Any thoughts?

About keeping myRoomExtension.jar in extensions/_lib_/:
My purpose to create room dynamically and attach the myRoomExtension.jar to it. By doing this, i guess multiple objects gets create automatically for each room. Is this right? And what you still prefer?

For now, myRoomExtension.jar contains the logic of gameType=1. In future, if need to implement another game type within same zone, i would need to keep another jar myRoomExtension_2.jar for gameType=2 logic. Is this right approach?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Error NoClassDefFoundError for interface

Postby Lapo » 26 Feb 2019, 16:30

MicroEyes wrote:Hi Lapo,
I'll try this. BUT If i keep common.jar outside my extension folder then the jar will be visible and accessible to my other project's extensions too. That's why i preferred to keep inside my extension folder.

If you keep the common.jar file in the SFS2X/extensions/_lib_/ folder it will be accessible from all your Extensions.
This is because the classes in that JAR are loaded from the top class loader, and therefore visible from everywhere.
http://docs2x.smartfoxserver.com/Extens ... assLoading

About keeping myRoomExtension.jar in extensions/_lib_/:
My purpose to create room dynamically and attach the myRoomExtension.jar to it. By doing this, i guess multiple objects gets create automatically for each room. Is this right? And what you still prefer?

Keeping it in your folder vs _lib_/ folder only affects the usage of Metaspace memory. Your Extension however will behave the same, regardless of the position.
You can learn more here:
https://smartfoxserver.com/blog/perform ... xtensions/
The article refers to PermGen memory which is the same equivalent to Metaspace since Java 8

For now, myRoomExtension.jar contains the logic of gameType=1. In future, if need to implement another game type within same zone, i would need to keep another jar myRoomExtension_2.jar for gameType=2 logic. Is this right approach?

Yes, it is a good approach.
This way you can separate each game logic in a different jar and update only what is needed.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: Error NoClassDefFoundError for interface

Postby MicroEyes » 26 Feb 2019, 16:51

Thanks, Lapo.

I have two questions:
1: First
Assume that I am working on 3 different SFS project, those are not related to each other:
1. project_1 with extension SFS2X/extensions/project_1/project_1_Extension.jar
2. project_2 with extension SFS2X/extensions/project_2/project_2_Extension.jar
3. project_3 with extension SFS2X/extensions/project_3/project_3_Extension.jar

By deploying common.jar file in the SFS2X/extensions/_lib_/ will make common.jar available to all.

2: Second
As per SFS structure, it's mandatory to put shared jar in extensions/_lib_/ folder, and I got this part. BUT what's the use of folder SFS2X/extensions/project_1/_lib_/? From you first reply, you said "the problem is that the _lib_ folder is located directly under SFS2X/extensions/. The one you have created in your own Extension folder will be ignored." .
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Error NoClassDefFoundError for interface

Postby Lapo » 26 Feb 2019, 17:04

MicroEyes wrote:Thanks, Lapo.

I have two questions:
1: First
Assume that I am working on 3 different SFS project, those are not related to each other:
1. project_1 with extension SFS2X/extensions/project_1/project_1_Extension.jar
2. project_2 with extension SFS2X/extensions/project_2/project_2_Extension.jar
3. project_3 with extension SFS2X/extensions/project_3/project_3_Extension.jar

By deploying common.jar file in the SFS2X/extensions/_lib_/ will make common.jar available to all.

Yes
2: Second
As per SFS structure, it's mandatory to put shared jar in extensions/_lib_/ folder, and I got this part. BUT what's the use of folder SFS2X/extensions/project_1/_lib_/?

There is no use. :) In our documentation we never mention such subfolder.
Any subfolder under your Extension directory will be ignored by SFS2X
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: Error NoClassDefFoundError for interface

Postby MicroEyes » 26 Feb 2019, 17:09

Great. Thanks a lot. I can finally resume my work ahead.

Appreciated for your help.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Edwardpen, Stevenor and 82 guests