3

I want to load my own java.lang.String

public class StringCustomClassFactory {
    public static String newInstance() {
        URLClassLoader tmp = new URLClassLoader(new URL[] {new URL("file:///home/.../target/classes/") }) {
            public Class loadClass(String name) {
                if ("java.lang.String".equals(name))
                        return findClass(name);
                    return super.loadClass(name);
                    //exception handling is omitted 
            }
        };
return (String) tmp.loadClass("java.lang.String").newInstance();
//...

But I constantly receive exception:

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
  • In this tutorial I've read that it possible to load your own SecurityManager

The security of the system can be compromised if your class loader returned its own value of java.lang.SecurityManager, which did not have the same checks as the real one did.

  • And here I have read that it possible to disable SecurityManger

What would you recommend me to solve this problem and is it solvable at all?

Community
  • 1
  • 1
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • First of all you should try to explain _why_ you need to do this, perhaps there are alternatives like AOP. – Marged Dec 07 '15 at 10:45

1 Answers1

1

You cannot, and never will be able to, override classes in a package starting with java.; it has nothing to do with a SecurityManager, but instead it's enforced in ClassLoader::preDefineClass

Considering it means something is Very Wrong(tm) with the line of reasoning you're following; could you explain exactly why you need this?

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40