0

I found this question -> Import custom libraries in Java

And @Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.

Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?

Can someone please tell me how we could deal with this?

EDIT: I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.

This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass

Community
  • 1
  • 1
MightyPixel
  • 111
  • 1
  • 8

3 Answers3

0

It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.

You can always use classes from every package, them being default or not, you can always mix.

Rens Verhage
  • 5,688
  • 4
  • 33
  • 51
  • How I can go around this? Given the stdlib.jar for instance witch uses the default package - Can I encapsulate it in some package? Thanks! – MightyPixel Feb 10 '13 at 17:43
  • Do you really need to? I assume you are organizing your own classes in packages. Do you have some example code where you're having problems? – Rens Verhage Feb 10 '13 at 17:45
  • Well I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore. This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass – MightyPixel Feb 10 '13 at 18:00
0

Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.

Importing classes inside JAR files that are in the default package

Community
  • 1
  • 1
57yu
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Kevin Feb 03 '15 at 02:24
0

I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package. You cannot import a class from a default package, basically, since the import operation needs the package name:

import packagename.*;

So there are only two way to fix this problem:

  1. the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically. Like this: enter image description here

  2. try to create your own jar file with a named package and copy all the class file into your newly-created jar file. Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff. Hope this would help you!

Dai Niu
  • 479
  • 7
  • 9