9

I've been looking all over the internet and Can't find an answer.

I'm using Eclipse and need to import JFrame from javax.swing. But hovering over the the declaration (which in Eclipse should give you an option to import it) the import option does not show up. Instead I manually typed out the import path, but get an error.

Going even further, I used the package explorer to attempt to fine it... couldn't. I have the latest version of Eclipse, and the Latest JRE and JDK. But still is not working.

Code:

package com.BickDev.Game;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 320;
    public static final int HEIGHT = WIDTH / 12 * 9;
    public static final int SCALE = 2;
    public final String TITLE = "Troy's Game Test";
    private boolean running = false;


    public void run() {

    }

    public static void main(String args[]) {

        Game game = new Game();
        Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
        game.setPreferredSize(size);
        game.setMaximumSize(size);
        game.setMinimumSize(size);

        JFrame frame = new JFrame(game.TITLE);
    }
}

the import javax.swing.JFrame now gives the error

Access restriction: The type JFrame is not accessible due to restriction on required library C:\ProgramFiles\Java\jre8\lib\rt.jar

No idea what this means...

Please help....

*UPDATE found the JFrame class.. but cannot access it.

Charles
  • 48,924
  • 13
  • 96
  • 136
UNEVERIS
  • 159
  • 1
  • 1
  • 7
  • Have you ever modified the permissions for that directory? – BitNinja Apr 22 '14 at 02:04
  • I have not modified any permissions intentionally – UNEVERIS Apr 22 '14 at 02:06
  • 1
    Have you checked [this](http://stackoverflow.com/questions/860187/access-restriction-on-class-due-to-restriction-on-required-library-rt-jar) and [this](http://stackoverflow.com/questions/9266632/access-restriction-is-not-accessible-due-to-restriction-on-required-library) and [this](http://stackoverflow.com/questions/10428984/access-restriction-on-jdk1-7-jre-lib-rt-jar)? – MadProgrammer Apr 22 '14 at 02:10
  • I would have, but don't have capability to.. I'll use that as a last resort. But if there are any other ways to fix the problem... Not to MadProgrammer – UNEVERIS Apr 22 '14 at 02:10
  • 1
    Thanks madProgrammer. this solved it – UNEVERIS Apr 22 '14 at 02:12

3 Answers3

16

When you make a new java project at JRE choose "Use an execution environment JRE and from there select JavaSE-1.7 or 1.8 and just should solve the problem. I had the same problem like you before.

user3623053
  • 188
  • 1
  • 6
16

Just add requires java.desktop; to your module-info.java file.

module-info.java:

module your_project_name{
    requires java.desktop;
}
Abdalla Roda
  • 359
  • 2
  • 10
12
  1. Right-click on the project
  2. Select properties
  3. Java build path
  4. Library > Add Library > Add JRE SYSTEM Library
  5. Execution Environment
  6. Select JavaSE-1.7
  7. Finish
Austin Mullins
  • 6,869
  • 2
  • 31
  • 47
baderkhane
  • 308
  • 2
  • 10