I use this trick (having some third-party same package and class name in our class path), often overriding the functionality of third-party libraries if I cannot wrap them.
In any case, all system classes must be loaded into jvm before downloading the client program (our application). It's all about ClassLoader , a compiler that allows this, because in reality it doesn't care too much about all classes, but at run time, JVM loader classes will take these things into account. If you look at the default source code ClassLoader (oracle jdk). You can see that there will be a check that you should not run the package with java.
private ProtectionDomain preDefineClass(String name, ProtectionDomain protectionDomain) { ... if ((name != null) && name.startsWith("java.")) { throw new SecurityException("Prohibited package name: " + name.substring(0, name.lastIndexOf('.'))); } ... }
Even with well-known bite code tools such as javaassist , this behavior will be limited, they will check if the class can be instrumental or not Instrumentation.isModifiableClass .
Finally, it all depends on the implementation of ClassLoader and, since it must adhere to the specifications, you need to consider the order of the classes from the class path to load.
source share