简体   繁体   English

Netbeans在创建匿名子类的内部类的匿名子类时找不到主类

[英]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. 当我尝试在其包含类的包含类的匿名子类的初始化中创建一个新的匿名Action子类时,Netbeans在运行时突然找不到主类,尽管能够进行清理+构建没有问题,并且可以与此运行代码已注释掉。 script.new Action(0){...}在运行时导致“错误:找不到或加载主类”注释掉代码可以成功运行

Code structure: 代码结构:

Main package: 主包装:

  • Main class <-- currently looking at this file 主类<-当前正在查看此文件
    • public void run(...) (called in main(String[] args)) public void run(...)(在main(String [] args)中调用)
      • Actor a = new Actor() { 演员a =新演员(){
        • Script script = new Script(); 脚本脚本= new Script();
        • { (Actor instance initiation code) {(演员实例启动代码)
          • script.new Action(0) {...} causes breakage script.new Action(0){...}导致损坏
  • 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: 有趣的是,-XX:+ PrintCompilation揭示了崩溃之前运行的某些内容:

     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; 产品版本: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; Java HotSpot(TM)64位服务器VM 23.25-b01运行时:Java(TM)SE Runtime Environment 1.7.0_25-b17系统:在amd64上运行的Windows 7版本6.1; Cp1252; cp1252; en_US (nb) zh_(nb)

Cleaning and building and restarting Netbeans have not resolved the problem. 清理,构建和重新启动Netbeans尚未解决问题。 Is this fixable or a bug in Netbeans? 这是可修复的还是Netbeans中的错误?

I was able to reproduce the issue in NetBeans 7.3.1. 我能够在NetBeans 7.3.1中重现该问题。 The problem appears to be related to bug #224770 . 该问题似乎与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. 修复摘要为#224770:使带有封闭表达式的new处理更类似于vanilla javac,同时保留API的正确输出。

You have two options. 您有两个选择。

  1. Upgrade NetBeans to 7.4 or newer. 将NetBeans升级到7.4或更高版本。 I tested the code in 7.4 and it worked properly. 我在7.4中测试了代码,它工作正常。
  2. Keep using NetBeans 7.3, and don't use "this.new". 继续使用NetBeans 7.3,不要使用“ this.new”。 Change line 11 to this: 将第11行更改为:

    B = new Inner2() { B =新的Inner2(){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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