【问题标题】:Why doesn't NetBeans find package com.apple.eawt when doing Clean and Build?为什么 NetBeans 在执行 Clean and Build 时找不到包 com.apple.eawt?
【发布时间】:2016-07-03 20:49:40
【问题描述】:

我的 NetBeans IDE 在我选择 Clean and Build Project 时响应“com.apple.eawt 包不存在”,但在我选择 Run Project 时工作正常。

我有一个针对 OS X 的 Java 8 应用程序。要在用户从菜单中选择退出时触发窗口侦听器,我有以下代码:

import com.apple.eawt.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppleUI extends JFrame {

public AppleUI() {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(400, 200));
    this.add(panel);

    Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.out.println("Quitting");
            System.exit(0);
        }
    });

    this.setVisible(true);
    this.pack();
}

public static void main(String[] args) {
    new AppleUI();
}
}

当我在 NetBeans 中选择运行项目时,应用程序运行良好。但是,当我选择 Clean and Build Project 或 Test Project 时,我收到以下错误:“package com.apple.eawt does not exist”。

因此,我也收到 Application 和 QuitStrategy 的“找不到符号”错误。

我觉得这很奇怪。 com.apple.eawt 包与所有其他 Java 运行时类一起位于 rt.jar 中。为什么在执行 Clean and Build 时 NetBeans 不能识别这一点?我做错了什么?

我使用的是 JDK 1.8.0_91 和 NetBeans IDE 8.1。感谢您的帮助!

【问题讨论】:

  • 如果是第三方库,那么您应该将该库添加到您的 netbeans 项目中
  • @Shashanth 该软件包包含在 Apple 发行版的标准 rt.jar 文件中,所以我认为它不应该是必需的。不过我也试过了,没有成功。

标签: java macos netbeans


【解决方案1】:

回答我自己的问题...

经过大量研究,我发现编译器不是在 rt.jar 中查找包,而是在一个名为 ct.sym 的符号文件中查找,该文件仅包含标准 Java 包。

可以使用编译器选项-XDignore.symbol.file 忽略符号文件,但这似乎不是推荐的解决方案。

我改写了代码,不使用 com.apple.eawt 包,而是使用系统属性。

//Application.getApplication().setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
System.setProperty("apple.eawt.quitStrategy", "CLOSE_ALL_WINDOWS");

另见here

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2021-07-30
    • 2012-06-26
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多