ores ores - 5 months ago 25
Java Question

Why doesn't NetBeans find package com.apple.eawt when doing Clean and Build?

My NetBeans IDE responds "package com.apple.eawt doesn’t exist" when I select Clean and Build Project but works fine when I select Run Project.

I have a Java 8 application targeting OS X. To trigger the Window Listener when the user selects Quit from the menu I have the following code:

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();
}
}


The application works fine when I select Run Project in NetBeans. However, when I select Clean and Build Project or Test Project I get the following error: ”package com.apple.eawt does not exist”.

Consequently I get ”cannot find symbol” errors for Application and QuitStrategy as well.

I find this very strange. The package com.apple.eawt is located in rt.jar together with all other Java runtime classes. Why doesn’t NetBeans recognize this when doing Clean and Build? What do I do wrong?

I’m using JDK 1.8.0_91 and NetBeans IDE 8.1. Thanks for help!

Answer

Answering my own question...

After a lot of research I found out that the compiler isn't looking for the package in the rt.jar but in a symbol file called ct.sym, which only contains the standard Java packages.

It is possible to ignore the symbol file using the compiler option -XDignore.symbol.file, however this seems not to be the recommended solution.

Instead I have rewritten the code in a way that I don't use the com.apple.eawt package but system properties.

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

See also here.