Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Extremely confusing NoClassDefFoundError problem  (Read 2881 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2012-01-01 22:05:01 »

Hello. I was just minding my own business with an inner class and BOOM, NoClassDefFoundError. I've done nothing wrong or weird, but it keeps throwing it at me every time I use the inner class!

The line where the error is thrown:
1  
2  
3  
ScriptFunctions.queuePathfinding(new Callback(unit), unit.getX(), unit.getY(),
         width * (0.5 - unitArea/2) + Math.random()*width * unitArea,
         height * (0.5 - unitArea/2) + Math.random() * height * unitArea);


queuePathfinding is defined as the following:
1  
public static void queuePathfinding(PathfinderCallback callback, double startX, double startY, double destX, double destY)


PathfinderCallback is just a simple interface:
1  
2  
3  
public interface PathfinderCallback { 
   public void result(ScriptPath path);
}

(I removed the package line)

Callback is just a simple inner class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    private class Callback implements PathfinderCallback{
     
      public ScriptUnit unit;
     
      public Callback(ScriptUnit unit){
         this.unit = unit;
      }

      @Override
      public void result(ScriptPath path) {
         if(path.found()){
            unit.setState(path);
         }
      }
   }


Everything compiles fine and if I comment out the failing line, everything works fine. Even just
1  
new Callback(unit);

crashes! I've also made sure that the extra .class file for the inner class is being generated! What the hell is wrong?!

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-01 23:35:58 »

What IDE are you using?

Offline ReBirth
« Reply #2 - Posted 2012-01-02 01:16:00 »

My bet is it has something to do with package, or your inner class can't find either ScriptPath or ScriptUnit. Exception print out can be helpful.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-01-02 13:55:35 »

What IDE are you using?
Eclipse.

My bet is it has something to do with package, or your inner class can't find either ScriptPath or ScriptUnit. Exception print out can be helpful.
1  
2  
java.lang.NoClassDefFoundError: dam/testscripts/DebugPathfindingTick$Callback
   at dam.testscripts.DebugPathfindingTick.tick(DebugPathfindingTick.java:30)

 which refers to the ScriptFunctions.queuePathfinding(new Callback(unit), ...); line, or the new Callback(unit); line, regardless of which I use.

I've been looking through the package names, but shouldn't I be getting an error in the editor if the package doesn't match the folder.

(PS: I'm feeling terrible at the moment, so I'm pretty sure that my debugging skills are severely compromised... >_>)

Myomyomyo.
Offline Waterwolf

Junior Member


Medals: 3



« Reply #4 - Posted 2012-01-02 14:01:46 »

Eclipse somehow stops compiling classes in specific project for some weird reason (have had it a couple of times and only way to fix it was to make a new project and copy sources to it) Maybe check the errors view of eclipse because they might prevent compiling yet keep old classes there.

Edit: didn't read you confirmed the subclass files are there. Hmm, no idea then :/
Offline theagentd
« Reply #5 - Posted 2012-01-02 14:15:04 »

I bypassed the Callback error by passing null instead of new Callback(unit) to it, but then it crashed when creating a pathfinding Job, which is also an inner class. Note that I am using inner classes more than 20 times in my game, and now it suddenly hates me...

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-01-02 14:27:36 »

I know it's barely helpful, but this is so odd that you should do a complete rebuild of your workspace [Projects -> Clean... -> All projects].

Check whether you have errors in your build path [right-click a project -> Build Path -> Configure build path...], check for cyclic dependencies or missing JARs in all (parent) projects.

Further you should check whether you have exported a JAR and got that same JAR in your classpath somehow, causing the classes to be loaded from your JAR as opposed to the workspace.

Ramble ramble ramble.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-01-02 16:36:31 »

@Riven The clean idea is the best course of action right now. Weird dependencies or missing JARs shouldn't affect anything really. Even if he has an older JAR, the newer class file should still be there in bin.

@theagentd
In the bin folder, can you find the OuterClas$Callback.class file inside its package folder?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-01-02 16:54:22 »

Weird dependencies or missing JARs shouldn't affect anything really
It does.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #9 - Posted 2012-01-02 22:53:05 »

I know it's barely helpful, but this is so odd that you should do a complete rebuild of your workspace [Projects -> Clean... -> All projects].

Check whether you have errors in your build path [right-click a project -> Build Path -> Configure build path...], check for cyclic dependencies or missing JARs in all (parent) projects.

Further you should check whether you have exported a JAR and got that same JAR in your classpath somehow, causing the classes to be loaded from your JAR as opposed to the workspace.

Ramble ramble ramble.

 - Clean didn't help...
 - No errors in build path...
 - I don't even know how to export as a JAR in Eclipse, so no...

@Riven The clean idea is the best course of action right now. Weird dependencies or missing JARs shouldn't affect anything really. Even if he has an older JAR, the newer class file should still be there in bin.

@theagentd
In the bin folder, can you find the OuterClas$Callback.class file inside its package folder?
1  
2  
DebugPathfindingTick$Callback.class
DebugPathfindingTick.class

Yes, the class file is there.

The funniest thing just happened. If I call the exact same from another class it works. Turns out the problem is my custom security manager.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
   private static class ScriptSecurityManager extends SecurityManager {

      private static String ERROR_MESSAGE = "Script security error. Permission requested: ";
      private boolean security = false;

      @Override
      public void checkPermission(Permission perm) {
         checkSecurity(perm);
         // super.checkPermission(perm);
     }

      @Override
      public void checkPermission(Permission perm, Object context) {
         checkSecurity(perm);
         // super.checkPermission(perm, context);
     }

      private void checkSecurity(Permission perm) {
         if (security && scriptThread == Thread.currentThread()) {
            throw new SecurityException(ERROR_MESSAGE + perm.toString());
         }
      }
   }

   public static void enableSecurity(){
      scriptThread = Thread.currentThread();
      securityManager.security = true;
   }
   
   public static void disableSecurity(){
      securityManager.security = false;
      scriptThread = null;
   }

If security = false, it works. If it's true, usage of inner/anonymous classes throw a NoClassDefFoundError.

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-01-02 22:55:45 »

Why even use a custom SecurityManager?! O_o

Offline theagentd
« Reply #11 - Posted 2012-01-02 23:02:32 »

My game will contain "scripts" written in Java. The custom SecurityManager was just a cheap attempt at preventing people from writing new File("C:/Windows/").delete();. I will be using a custom ClassLoader containing a whitelist with allowed classes plus a package where the scripter can put his scripts in. Would a custom ClassLoader be enough for securely running some Java code?

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-01-02 23:11:29 »

Would a custom ClassLoader be enough for securely running some Java code?
A script thread can create a new thread and (hence) drop all security restrictions of your SecurityManager.

This is far from a trivial problem. I had a 'safe' implementation that relied on ThreadGroups (as you can't 'escape' them) but I had to disable access to any code that took a Runnable and executed it on a Thread that didn't belong to the ThreadGroup of the script (even SwingUtilities.invokeLater(..)). You're also not safe from code that keeps the CPU busy or allocates (and never releases) a lot of (native) memory. It can also deadlock your app when it somehow has access to a mutex.

If you want to make it a tad safer, you have to make a snapshot of the objects that the script is allowed to see. Once the script is done, you check the state of the snapshot and copy the valid modifications back into your game objects.

It might even be most secure to interface the script with the engine over tcp in a seperate (sandboxed) process. Maybe the overhead is... acceptable.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #13 - Posted 2012-01-03 07:56:29 »

Would a custom ClassLoader be enough for securely running some Java code?
A script thread can create a new thread and (hence) drop all security restrictions of your SecurityManager.

This is far from a trivial problem. I had a 'safe' implementation that relied on ThreadGroups (as you can't 'escape' them) but I had to disable access to any code that took a Runnable and executed it on a Thread that didn't belong to the ThreadGroup of the script (even SwingUtilities.invokeLater(..)). You're also not safe from code that keeps the CPU busy or allocates (and never releases) a lot of (native) memory. It can also deadlock your app when it somehow has access to a mutex.

If you want to make it a tad safer, you have to make a snapshot of the objects that the script is allowed to see. Once the script is done, you check the state of the snapshot and copy the valid modifications back into your game objects.

It might even be most secure to interface the script with the engine over tcp in a seperate (sandboxed) process. Maybe the overhead is... acceptable.
But if I have a custom ClassLoader that refuses to load any class not on a whitelist (like Runnable or Thread, obviously) would it really be possible to escape the SecurityManager? Performance is also VERY important here, since I am unable to optimize user scripts or even add multithreading due to the flexibility needed, I expect scripts to be a major bottleneck when there are lots of objects in the game...

I also found the exact source of the problem. It turns out that Java requests a FilePermission when it loads .class files (well, it should have been obvious...). It loaded the main script class when the game was started since a reference to the debug script was in the game code, but the inner class of the "script" wasn't loaded until the "script" was actually run, meaning it tried to load the inner class (DebugPathfindingTick$Callback.class), which my SecurityManager did not allow. The exception it threw was then silently eaten by the ClassLoader. The reason I didn't notice it was because I had 300 NoClassDefFoundErrors being printed per second, and Eclipse only stores the most recently printed text in the console... >_> Still not used to Eclipse...

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 337
Projects: 2
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-01-03 09:35:13 »

There is a limit to the number of lines in the Console, which you can increase if you need.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #15 - Posted 2012-01-03 15:03:00 »

The reason I didn't notice it was because I had 300 NoClassDefFoundErrors being printed per second

Stop writing such buggy code!   Tongue
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (45 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!