Exception in thread “main” java.lang.SecurityException: Prohibited package name: java.lang

China☆狼群 提交于 2019-12-10 14:05:26

问题


I'm a newbie at Java and I have a program that is returning the following error that I am completely unable to figure out. I googled and everything. Could you guys help me?

package java.lang;
public class S1 {
public static void main(String[] args) {
    for (int i=1;i<=1000;i++)
        {
            String str = "1" +i;
        }
    }
}

 

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
    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)

I am using Eclipse, and am working with the package java.lang in the file S1.java.


回答1:


You can't place new content into the java.lang package. It's reserved by the language because that's where the core Java content already resides. In fact, everything within the java.lang package is implicitly imported by default, in any piece of Java code.

It contains "classes that are fundamental to the design of the Java programming language." (from the docs). Since user-defined classes cannot, by definition, be critical to the design of the language, you are forbidden from putting content there. Allowing users to put code within the java.lang package would also be a problem because that would expose any package-domain content defined there to the user.

Just change your package name (to virtually anything else), and you'll be good to go. By convention, package names are usually lowercase, but you can make it whatever makes sense for your project. See the tutorial on packages for more.




回答2:


Right click on your package click reflactor and then rename it..... Because you used internal variable.. See the image below..




回答3:


java.lang is system package from jvm which is implicitly imported to java source so its already taken you can't have same package name




回答4:


You package name coincides with the "internal" package provided in java.




回答5:


User code is never allowed to put classes into one of the standard Java packages. That way, user code cannot access any package-private classes/methods/fields in the Java implementation. Some of those package-private objects allow access to JVM internals.

@credit Chris Jester-Young



来源:https://stackoverflow.com/questions/17354275/exception-in-thread-main-java-lang-securityexception-prohibited-package-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!