Extremely confusing NoClassDefFoundError problem

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:

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:

public static void queuePathfinding(PathfinderCallback callback, double startX, double startY, double destX, double destY)

PathfinderCallback is just a simple interface:

public interface PathfinderCallback { 
	public void result(ScriptPath path);
}

(I removed the package line)

Callback is just a simple inner class:

    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

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?!

What IDE are you using?

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.

Eclipse.

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… >_>)

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 :confused:

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…

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.

@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?

It does.

  • Clean didn’t help…
  • No errors in build path…
  • I don’t even know how to export as a JAR in Eclipse, so no…
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.


	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.

Why even use a custom SecurityManager?! O_o

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?

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…

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

Stop writing such buggy code! :stuck_out_tongue: