4

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:

  • Main class <-- currently looking at this file
    • public void run(...) (called in main(String[] args))
      • Actor a = new Actor() {
        • Script script = new Script();
        • { (Actor instance initiation code)
          • script.new Action(0) {...} causes breakage
  • Package actor
    • public abstract class Actor
      • public class Script
        • public abstract class Action

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?

Vitruvie
  • 2,327
  • 18
  • 25
  • It would really help if you could provide a short but complete program demonstrating the problem. – Jon Skeet Dec 19 '14 at 08:18
  • Presumably with a package statement, right? – Jon Skeet Dec 19 '14 at 08:31
  • Hmm. I don't have Netbeans installed so can't reproduce it myself - but it works fine with `javac`. Out of interest, can you find the compiled `.class` files? Can you launch it from the command line without any problems? – Jon Skeet Dec 19 '14 at 08:32
  • @JonSkeet The class files are right there, and the jar Netbeans built runs fine from the command line. – Vitruvie Dec 19 '14 at 08:42
  • Weird. Thanks for the extra info - sorry I couldn't be more helpful :( – Jon Skeet Dec 19 '14 at 08:47
  • The main class meant in the error refers to a missing class with `public static void main(String[] args)`. I did not see the main method mentioned. – Joop Eggen Dec 19 '14 at 08:56
  • @JoopEggen The main method is not shown in the screenshots but it is present, as it is in the ClassTester class which has the same problem. – Vitruvie Dec 19 '14 at 08:57
  • In the case of ClassTester, the name containing "Test" categorizes the source as unit test by default. Refactor - Rename it to something else. In my NetBeans "Run File" was disabled for ClassTester and not for ClassJester. For the moment avoid "test" – Joop Eggen Dec 19 '14 at 20:03
  • @JoopEggen Do note that commenting out the B = this.new Inner2(){...} line causes the program to run fine (until of course the NPE). Refactoring it to jests.ClassJester produces the same error. "Run File" is not disabled for me, this may be because it is within my "Tests" project of which tests is the only package. The source of the problem is the creation of an anonymous subclass of Inner2 within A. – Vitruvie Dec 20 '14 at 00:09

1 Answers1

1

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() {

Corey
  • 664
  • 9
  • 18
  • Option 2 doesn't work in the actual unsimplified code because there are 3 classes and the enclosing class is not the current class; however Option 1 worked. I didn't even know that Help->Check for Updates wouldn't tell me about major version changes. – Vitruvie Dec 23 '14 at 20:20