简体   繁体   中英

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){...}在运行时导致“错误:找不到或加载主类”注释掉代码可以成功运行

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM