2

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?

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • Similar to http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar – David Gelhar Jan 06 '11 at 03:11
  • @David: I don't understand how that question help me. `JFrame` is a standard class in Java SE. – Jonas Jan 06 '11 at 10:50
  • Did you post the entire `Test` class? Are you using the empty package or maybe `package javas.swing;`? – Mot Jan 06 '11 at 11:21
  • @mklhmnn: yes, it's the entire program. I get the same error if I use `package com.example`. – Jonas Jan 06 '11 at 11:31
  • @Jonas the answer to that question suggests that the error message can result from a jar file in your classpath that's trying to replace a standard class. – David Gelhar Jan 06 '11 at 14:20
  • David, this is a good idea! Maybe there is another javax.swing.JFrame in the classpath which you try to invoke, but hides the default one. – Mot Jan 06 '11 at 16:28
  • @David: I haven't included any jar-files. Is there any specific settings I have to do in Eclipse to be able to use Java Swing on Mac? – Jonas Jan 06 '11 at 16:30
  • @mklhmnn: I don't understand this, but is there any way I can solve it? The program is very minimal, it's just that I can't import Java SE standard classes when I use Java on Mac OS X. – Jonas Jan 06 '11 at 16:32
  • 1
    Unfortunately, I can't help you with Eclipse, because I'm using a real Java-IDE: IntelliJ. ;) What happens if you try to launch it from command line? – Mot Jan 06 '11 at 16:53
  • @mklhmnn: Good suggestion. I can compile and run the application in the Terminal, so it seems to be a specific problem with Eclipse. Maybe I should start to use IntelliJ then, it's often I have problems with Eclipse. – Jonas Jan 06 '11 at 17:23

2 Answers2

3

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'm using Xcode 3.2.5. The computer is brand new and I have updated all software. – Jonas Jan 07 '11 at 01:16
  • 2
    Under `Eclipse > Preferences > Java > Installed JREs` the JVM 1.4 was selected, I selected JVM 1.6.0 instead and created a new project. Now is it working. Thanks. – Jonas Jan 07 '11 at 01:25
0

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.

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49