tags:

views:

59

answers:

2

I have a Java application and the Java files are in a package called app. I have added a JAR file to the classpath. All classes in the JAR file are in the default package.

From my Java application I can't access classes of this JAR file. Only if I put my application files in the default package, then I can see the classes in the JAR file. Why is that?

+1  A: 

Classes in the default package are not visible to classes inside a package.

When classes are in a different package, then you basically need to import classes. But since it's impossible to import classes in the default package, the only solution is to put those classes in a package.

Always put classes in a package whenever you want to reuse classes.

BalusC
+1  A: 

You can no longer import from default packages. (You used to be able to in Sun's early javac implementations, but this was not allowed for in the language spec.) Obvious solutions:

  • Don't use the default package, if possible. Move all classes, interfaces and enums into named packages. [good]
  • Move your packaged classes into the default package. [bad]
  • Access the classes via reflection. [terrible]
Tom Hawtin - tackline
I have tryed to make a jar with the classes I need into a package, but I can't see the classes from my application
Giovanni
@Giovanni I think you are going to need a simple but complete example of what you are trying to do.
Tom Hawtin - tackline