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

javaruntime-error

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.

Best Answer

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.

Related Topic