Yusuf
Yusuf

Reputation: 119

Java class name that starts with java

I have a java class that I am importing unfortunately named java.net_y. My code compiles, but when I try to run it I get:

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

I've read other threads about trying to override core java classes like java.io.....thats not what I am doing....this external class I have is just named badly.

Another thread mentioned reflection...not sure if that is required.

What can I do?

Upvotes: 1

Views: 346

Answers (4)

AlexR
AlexR

Reputation: 115328

I should rename your class. You cannot put classes to java package. See

java.lang.ClassLoader#predefineClass()

This is a private method that performs the following check:

if ((name != null) && name.startsWith("java.")) {
    throw new SecurityException("Prohibited package name: " +
                name.substring(0, name.lastIndexOf('.')));
}

As far as I understand this is exactly the exception you see.

Sometimes people have to override classes from JDK. It is not your case, but I'd like to mention it just in case. To do this you have to put this overridden class to

-Xbootclasspath

instead of regular

-classpath

when you are running your JVM

Upvotes: 0

Guillaume
Guillaume

Reputation: 14656

If the class was generated from a web service WSDL, the best thing would be to regenerate it and forcing the package name to something else. Most WSDL-to-java tools have parameters to let you do that.

Upvotes: 2

npinti
npinti

Reputation: 52185

What you can do is to use Java Decompiler to reverse engineer what you are importing, make the necessary changes and then recompile.

I am actually assuming that you are trying to import some sort of .jar file or something along those lines. If you are importing just one class, then all you need to do is to rename it.

Upvotes: 0

Stan Kurilin
Stan Kurilin

Reputation: 15792

What can I do?

Rename that class.

Upvotes: 5

Related Questions