0

I've just begun to create a game using Java. Problem, being it will not run due to this error:

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.maze.game
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

It says that a java exception has occurred, but all my other programs will run. I've tried:

  • Uninstalling Java and Re-installing
  • Running the most updated Eclipse program (I run Juno)

Here is my code:

package java.maze.game;

import java.awt.BorderLayout;
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 = 160;
public static final int HEIGHT = WIDTH/12*9;
public static final int SCALE = 3;
public static final String NAME = "Maze game";

public boolean running = false;

private JFrame frame;

public Game(){
    setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));   //sets the minimum size of the frame
    setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));   //sets the maximum size of the frame
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); //sets the preferred size of the frame

    frame = new JFrame(NAME);   //creates the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //set the default for what happens when the program is exited

    frame.setLayout(new BorderLayout());    //creating a new layout for the frame
    frame.add(this, BorderLayout.CENTER);   //keeps everything centered not overlapping the taskbar

    frame.pack();   //sets the frame above or at the preferred size
    frame.setResizable(false);  //makes sure the user can't resize the frame
    frame.setLocationRelativeTo(null);  //we don't want the location of the screen relative to any variable
    frame.setVisible(true); //sets the frame to visible
}

public void start() {
    running = true;
    new Thread(this).start(); //whenever the Thread is started it will run the run(); method

}
public void stop() {
    running = false;

}

public void run() {
    while(running){
        System.out.println("It worked!");
    }
}

public static void main(String[] args){
    new Game().start();
}
}

Thank you!

EDIT Feel like an idiot now, but thank you again.

Hoberfinkle
  • 35
  • 1
  • 7

3 Answers3

2

I dont think you can use java as your package name.

package java.maze.game;

Rename it and try to compile again.

chriskvik
  • 1,281
  • 2
  • 10
  • 32
2

As the exceptions says, you are not allowed to name your package to something starting with java.*.

Renaming and rebuilding should solve your issue.

And as Mike W says, this question has already been asked here:

Community
  • 1
  • 1
Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
2

You can't use the word java to start your own packages

"java.maze.game;" //this is not valid

Try something else and it would run.

Best regards.

code4jhon
  • 5,725
  • 9
  • 40
  • 60