0
package java.basic;

public class Test {

    public static void main(String[] args) {

        int जानिए = 7;

        System.out.println(जानिए);

    }


}

Above class is compiling but after running getting exception

java.lang.SecurityException: Prohibited package name: java.basic
    at java.lang.ClassLoader.preDefineClass(Unknown Source)
    at java.lang.ClassLoader.defineClassCond(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$000(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)
Exception in thread "main" 
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Neel Salpe
  • 1,287
  • 3
  • 15
  • 26

4 Answers4

2

See the docs:

Packages in the Java language itself begin with java. or javax.

java. is not for you, the English alphabet has 26 letters. Use them to generate names other than java.

Also see java.lang:

Provides classes that are fundamental to the design of the Java programming language.

It's for Java code, and it's imported by default in every code.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

Because otherwise your classes will get access to default (no modifiers) / protected members of standard JDK classes which are not meant to expose its internal details to the developer.

Prohibiting custom classes in java.* packages forbids you from exploiting such thing.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

Allowing the use of a user created package named java would allow access to package-private fields, methods and constructors.

Troubleshoot
  • 1,816
  • 1
  • 12
  • 19
1

java is a reserved package name. Only classes inside the JVM can reside in this package.

If anyone could write in the java package, that could result in libraries arbitrarily replacing core Java classes with their own implementations. That could result in a lot of things, from breaking core Java features to execution of malicious code.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55