线程“main”中的异常java.lang.SecurityException:禁止包名:java.lang

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17354275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:46:37  来源:igfitidea点击:

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

javaruntime-error

提问by Henry Keiter

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?

我是 Java 的新手,我有一个程序返回以下错误,我完全无法弄清楚。我用谷歌搜索了一切。你们能帮我吗?

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.access0(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.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.langin the file S1.java.

我正在使用 Eclipse,并且正在处理java.lang文件中的包S1.java

回答by Henry Keiter

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

您不能将新内容放入java.lang包中。它由语言保留,因为它是核心 Java 内容所在的位置。事实上,java.lang默认情况下,包中的所有内容都是隐式导入的,在任何一段 Java 代码中。

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.langpackage would also be a problem because that would expose any package-domain content defined there to the user.

它包含“对 Java 编程语言的设计至关重要的类”。(来自文档)。由于用户定义的类根据定义不能对语言的设计起到关键作用,因此禁止将内容放在那里。允许用户将代码放入java.lang包中也将是一个问题,因为这会向用户公开在那里定义的任何包域内容。

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 packagesfor more.

只需更改您的包名称(几乎可以更改任何其他名称),您就可以开始使用了。按照惯例,包名通常是小写的,但您可以使用对您的项目有意义的任何名称。有关更多信息,请参阅有关软件包教程

回答by vinod

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

右键单击您的包单击 reflactor,然后重命名它..... 因为您使用了内部变量.. 见下图..

enter image description here

在此处输入图片说明

回答by Jigar Joshi

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

java.lang是来自 jvm 的系统包,它被隐式导入到 java 源,所以它已经被采用了,你不能有相同的包名

回答by JHS

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

您的包名称与 java 中提供的“内部”包一致。

回答by lvarayut

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.

永远不允许用户代码将类放入标准 Java 包之一。这样,用户代码就无法访问 Java 实现中的任何包私有类/方法/字段。其中一些包私有对象允许访问 JVM 内部。

@credit Chris Jester-Young

@credit Chris Jester-Young