Rudziankoŭ
Rudziankoŭ

Reputation: 11251

Load your own SecurityManager or disable existing

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

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.

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

Upvotes: 3

Views: 81

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

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?

Upvotes: 1

Related Questions