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?
-
I don't know how you do it, but I just don't. – stevedbrown Jun 25 '09 at 21:25
-
7Nobody has any business putting classes in the default package. What library is this, just so I can avoid it? – Michael Myers Jun 25 '09 at 21:25
-
I'm actually not sure, a friend asked me how to do it so I came here for him. – Adam Lerman Jun 25 '09 at 21:27
-
8You open a bug with the library developer and tell them to put it in a package. – Rob Hruska Jun 25 '09 at 21:27
-
1Can't you just refer to them by name, once the jar is included in the classpath? Imports are for NOT using qualified names like blah.Hello so if there's no package name, it's just Hello. No? – Dan Rosenstark Jun 25 '09 at 21:32
-
1Sorry, I answered from faulty memory. I just tried it in Eclipse and wasn't able to get it to work. – Matt Poush Jun 25 '09 at 21:34
-
6i refer to them as 'poorly designed' – akf Jun 25 '09 at 21:35
-
2More information might be found here: http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package – Rob Hruska Jun 25 '09 at 21:52
-
1For those that think it just works, you were right in JDK 1.3.1. If you follow the link from @robhruska (or the answer below), you can see this capability was removed by Sun. – Jared Oberhaus Jun 25 '09 at 22:04
2 Answers
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.

- 14,547
- 4
- 56
- 55
-
My earlier answer was not correct; I made a mistake in my test program. This answer should be more correct. – Jared Oberhaus Jun 25 '09 at 22:02
To avoid the tedious method.invoke()
calls, I adapted the above solution:
Write an interface for the desired functionality in your desired my.package
package my.package;
public interface MyAdaptorInterface{
public void function1();
...
}
Write an adaptor in the default package
:
public class MyAdaptor implements my.package.MyAdaptorInterface{
public void function1(){thirdparty.function1();}
...
}
Use ClassLoader/Typecast to access object from my.package
package my.package;
Class clazz = ClassLoader.getSystemClassLoader().loadClass("MyAdaptor");
MyAdaptorInterface myObj = (MyAdaptorInterface)clazz.newInstance();
myObj.function1();

- 21
- 1