Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a custom java.* package?

Tags:

java

package

Can I create a package of my own that has the same name as a predefined package
in Java, such as java.lang?

If so, what would the results be? Wouldn't that enable me to access that package's protected members?

If not, what prevents me from doing so?

like image 831
Ibrahim Najjar Avatar asked Dec 16 '22 15:12

Ibrahim Najjar


2 Answers

No - java.lang is prohibited. The security manager doesn't allow "custom" classes in the java.lang package and there is no way telling him to accept them.

You're right - own classes declared in the java.lang namespace would allow you to use protected methods and members of classes in that package, and this is definitly not wanted.


This compiles fine - but - try to execute it ;)

package java.lang;

public class EvilAsEvilCanBe {

    public static void main(String[] args) {
        System.out.println("hehe");
    }

}
like image 50
Andreas Dolk Avatar answered Dec 28 '22 23:12

Andreas Dolk


Any package name matching "java.*" is prohibited and a security exception will be thrown.

like image 45
d-live Avatar answered Dec 29 '22 01:12

d-live