I am new to Mac OS X and is using version 10.6.5. JDK 1.6_u22 seems to be preinstalled on the system. I have downloaded Eclipse 3.5.2.
It works fine if I create a simple hello world, but I can not import JFrame
or use Swing. The error message that I get is:
Access restriction: The type JFrame is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
Here is the simple program that I have tried with:
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
new JFrame();
}
}
How do I use Java Swing in Eclipse on Mac OS X?
On Mac OS X 10.5.8, Eclipse 3.4.2 and Xcode 3.1.4, the example below builds and runs using recent revisions of either Java 1.5 or Java 1.6. As Xcode includes the JDK, what version of Mac OS X and Xcode are you using?
Also verify that your Swing project hasn't inadvertently included SWT.
Addendum: Check these two dialogs:
Eclipse > Preferences > Java > Build Path
Eclipse > Preferences > Java > Installed JREs
You should see references to /System/Library/Frameworks/JavaVM.framework/
.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class X {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel() {
private final int SIZE = 200;
private final int INSET = 20;
@Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
Line2D line1 = new Line2D.Double(INSET, INSET,
getWidth() - INSET, getHeight() - INSET);
Line2D line2 = new Line2D.Double(getWidth() - INSET,
INSET, INSET, getHeight() - INSET);
g2.setStroke(new BasicStroke(16,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2.draw(line1);
g2.draw(line2);
}
});
frame.pack();
frame.setVisible(true);
}
}
My best guess is that you included the runtime JAR files from Eclipse for JDK 1.6 but Eclipse is trying to run with JDK 1.5.
Eclipse uses SWT which relies on 32-bit binaries. The only 32-bit JVM on Mac OSX is 1.5 which means Eclipse has to run using JDK 1.5. It is plausible you set your project up to run with 1.5 and are trying to use 1.6 classes.jar
In other words verify Eclipse is launching your application with the correct JVM.