Vitruvie
Vitruvie

Reputation: 2327

Netbeans fails to find main class when creating anonymous subclass of inner class of anonymous subclass

When I attempt to create a new anonymous Action subclass inside the initialization of an anonymous subclass of its containing class's containing class, Netbeans suddenly fails to find the main class when running, despite being able to clean+build with no problem and to run with this code commented out. script.new Action(0) {...} causes "Error: Could not find or load main class" when running Commenting the code out leads to successful run

Code structure:

Main package:

Replicated in a simple class:

package tests;

public class ClassTester {
    public static void main(String[] args) {
        ClassTester tester = new ClassTester();
        tester.run();
    }
    public void run() {
        final Inner1 A = new Inner1() {
            {
                B = this.new Inner2() {
                    @Override
                    public void run() {
                        System.out.println("Hello, world!");
                    }
                };
            }
        };
        A.B.run();
    }
    public class Inner1 {
        public Inner2 B;
        public abstract class Inner2 implements Runnable {
        }
    }
}
-->
Error: Could not find or load main class tests.ClassTester
Java Result: 1

Interestingly, -XX:+PrintCompilation reveals that something runs before the crash:

     50    1             java.lang.String::hashCode (55 bytes)
     50    2             java.lang.String::charAt (29 bytes)
Error: Could not find or load main class tests.ClassTester
Java Result: 1

Product Version: NetBeans IDE 7.3.1 (Build 201306052037) Java: 1.7.0_25; Java HotSpot(TM) 64-Bit Server VM 23.25-b01 Runtime: Java(TM) SE Runtime Environment 1.7.0_25-b17 System: Windows 7 version 6.1 running on amd64; Cp1252; en_US (nb)

Cleaning and building and restarting Netbeans have not resolved the problem. Is this fixable or a bug in Netbeans?

Upvotes: 4

Views: 681

Answers (1)

Corey
Corey

Reputation: 704

I was able to reproduce the issue in NetBeans 7.3.1. The problem appears to be related to bug #224770. The fix summary is #224770: making handling of new with enclosing expression more similar to vanilla javac, while preserving the correct outputs from the API.

You have two options.

  1. Upgrade NetBeans to 7.4 or newer. I tested the code in 7.4 and it worked properly.
  2. Keep using NetBeans 7.3, and don't use "this.new". Change line 11 to this:

    B = new Inner2() {

Upvotes: 1

Related Questions