0

我刚刚开始使用 Java 创建游戏。问题,由于此错误而无法运行:

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)

它说发生了 java 异常,但我的所有其他程序都会运行。我试过了:

  • 卸载 Java 并重新安装
  • 运行最新的 Eclipse 程序(我运行 Juno)

这是我的代码:

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

谢谢!

编辑 现在感觉像个白痴,但再次感谢您。

4

3 回答 3

2

我不认为你可以使用 java 作为你的包名。

java .maze.game;

重命名它并尝试再次编译。

于 2013-11-03T00:56:07.313 回答
2

正如例外所说,你不能将你的包命名为以java.*开头的东西。

Renaming and rebuilding should solve your issue.

正如Mike W所说,这里已经提出了这个问题:

于 2013-11-03T00:57:57.903 回答
2

你不能用 java 这个词来启动你自己的包

“java.maze.game;” //这是无效的

尝试别的东西,它会运行。

此致。

于 2013-11-03T01:00:33.897 回答