views:

183

answers:

1

I have downloaded a third party library and they have classes I need to refer to in the default package? How do I import these classes?

+6  A: 

It's not possible directly with the compiler. Sun removed this capability. If something is in the default namespace, everything must be in the default namespace.

However, you can do it using the ClassLoader. Assuming the class is called Thirdparty, and it has a static method call doSomething(), you can execute it like this:

Class clazz = ClassLoader.getSystemClassLoader().loadClass("Thirdparty");
java.lang.reflect.Method method = clazz.getMethod("doSomething");
method.invoke(null);

This is tedious to say the least...

Long ago, sometime before Java 1.5, you used to be able to import Thirdparty; (a class from the unnamed/default namespace), but no longer. See this Java bug report. A bug report asking for a workaround to not being able to use classes from the default namespace suggests to use the JDK 1.3.1 compiler.

Jared Oberhaus
My earlier answer was not correct; I made a mistake in my test program. This answer should be more correct.
Jared Oberhaus