Can I create a custom java package. *?

Can I create my own package that has the same name as the predefined package
in Java e.g. java.lang ?

If so, what will be the results? Wouldn't that allow me to access these protected elements?

If not, what prevents me from doing this?

+4
source share
3 answers

No - java.lang prohibited. The security manager does not allow "custom" classes in the java.lang , and there is no way to tell it to accept them.

You are right - your own classes declared in the java.lang namespace will allow you to use the protected methods and class members in this package, and this is definitely not required.


This compiles fine, but - try executing it;)

 package java.lang; public class EvilAsEvilCanBe { public static void main(String[] args) { System.out.println("hehe"); } } 
+7
source

Any package name matching "java. *" Is denied and a security exception will be thrown.

+3
source

There are two things that bother you.

1) License agreement. "Note: Applications that use this parameter to override the class in rt.jar should not be deployed, as this is contrary to the Java 2 Runtime Environment binary license." http://download.oracle.com/javase/6/docs/technotes/tools/windows/java.html

2) You must use -Xbootclasspath:bootclasspath or add the lib / endorsed directory.

Some classes are not easily modified due to internal optimizations in the JVM, for example. you cannot add more than one method for Object to Sun / Oracle JVM;)

+2
source

All Articles