8

I have found information that java.* and javax.* are illegal(reserved) package names(in the book "OCA Java SE 7 Programmer I Study Guide"). When I try create package "java" and run class from it, I receive:

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java

but when I run class from "javax" package I receive no errors. On docs.oracle.com I've found only information:

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

so... is it "javax" illegal name or not? Maybe it's illegal only on Java EE, or older versions of Java?(I've tried it on JDK 1.6.0_43 and 1.7.0_25)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
SathOkh
  • 826
  • 2
  • 12
  • 24
  • http://stackoverflow.com/questions/727844/javax-vs-java-package – ZhongYu Jul 20 '13 at 16:12
  • 1
    Thanks, I've seen this thread already. It gives good information about javax package, but it doesn't say is it legal name for package or not. – SathOkh Jul 20 '13 at 16:29

1 Answers1

3

javax. is used for extensions (possibly within the JRE), so sure it is possible to define classes within those packages. IIRC, this can be disabled in untrusted contexts by adding javax. to the package.definition security property (not checked).

java. is special because ClassLoader prevents non-bootstrap class loader in those packages as an anti-Microsoft measure.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • I don't think Java is just special because of the classloader; it contains the official baseline API of Java. Do you have any references for this answer? – Maarten Bodewes Jul 20 '13 at 15:44
  • 1
    @owlstead `java/lang/ClassLoader.java`. Specifically `preDefineClass` method. – Tom Hawtin - tackline Jul 20 '13 at 15:51
  • 1
    @SathOkh Yes, otherwise it would not be possible to implement specific [JSR's](http://jcp.org/en/jsr/all). It is of course very unwise to put any *other* libraries (than the ones specified in a JSR) or applications in the `javax` namespace. The fact that it does not directly flag a compiler error does not mean that you should use it. – Maarten Bodewes Jul 20 '13 at 16:14