• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is any restriction of name package?

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is any restriction of name package?
like
+
Exception in thread "main" java.lang.SecurityException: Prohibited package name:
java
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 testining.Speak.main(Speak.java:19)

package javax have same exception. (only when use class from package/subpackage java or javax)
Is another (not java.*; or javax.*;) package names that restricted?
and one more is question in exam about purpose of basic package like java.awt and others?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35277
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java and javax are reserved for the JDK itself. If you could use them, you could create confusion about which code was being used or other types of problems.

AWT is not on the cert. For the OCA 7, only ArrayList in java.util (and a number of classes in java.lang) are on the exam. For the OCA 8, they add selected classes in java.util.functional and java.time.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created 3 classes in package java or a subpackkage:


And for every class a test class:


Output of these 3 test classes:
  • TestClass1: Exception in thread "main" java.lang.SecurityException: Prohibited package name: java
  • TestClass2: Exception in thread "main" java.lang.InstantiationError: java.util.List
  • TestClass3: Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.util


  • So based on the output of TestClass2 you can make the following conclusion: the system classes are searched first to find a matching class. That's why the JVM tries to instantiate the List interface (and not my List class).

    I can understand creating interfaces/classes inside java & javax packages can be confusing and therefore it's not allowed. But why do you get a runtime exception (when trying to use one of the above classes) and not a compiler error if you try to compile one of the above classes? A compiler error is fail fast (and more obvious/clear), a runtime exception is not. Just wondering...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic