Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help linking & packages

Doug Robinson
Guest
 
Posts: n/a
#1: Aug 14 '07
If I try to access another jar files default package I have no
problem. When I try to access anythig else i.e. package com.mine.thing,
I always get the not found message.

What special thing do I need to do to make it work?


package a.b.c.*;

import Another.*;
class Test {
System.out.println ("Hello World");
try {
Class.forName("another/Thing");
// Class.forName("Stuff");
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
System.out.println ("Good bye Cruel World");
System.exit (-1);
}

}
for this class Stuff works but another/Thing is another thing.

Any ideas?

Thanks for your time.

Mark Rafn
Guest
 
Posts: n/a
#2: Aug 14 '07

re: Need help linking & packages


Doug Robinson <dkrqXXX@XXXtelusplanet.netwrote:
Quote:
>If I try to access another jar files default package I have no
With the exception of "sealed packages", jars and packages are unrelated.
Jars can contain classes from multiple packagages, and classes in the same
package can live in multiple jars.

There is no "default package" for a jar. There is the or un-named package
that can have classes in it, but generally you shouldn't use it as it's a PITA
to mix named and un-named packages.
Quote:
>package a.b.c.*;
* isn't a valid package name for this class.
Quote:
>import Another.*;
This will make short names used in java code search the Another package for a
match. It's just a coding shortcut, and has no effect at runtime (it
doesn't even end up in your .class file). Since you have no java references
to any class, it will have no effect.

Oh, and by convention packages should start with lower-case letters. Classes
start with a capital. And it does matter - names are case-sensitive.
Quote:
>class Test {
System.out.println ("Hello World");
try {
Class.forName("another/Thing");
Class names don't have slashes in them. They have dots to separate package
elements and to separate package from classname. Packages and classes are
case-sensitive, too, so "Another" and "another" are different. Try
Class.forName("another.Thing"). Since this is a method call rather than java
code to be compiled, the imports have no effect and you must pass in the
fully-qualified classname.

But really, there's no reason to do this for most programs. Class.forName()
is only needed if you don't know when writing the code what the class name
will be.

You should just do:
Thing t = new Thing();
/* or whatever. If you haven't imported another.* or another.Thing,
you can say another.Thing t = new another.Thing(); */
Quote:
>// Class.forName("Stuff");
This is an example of trying to use the unnamed package to look for Stuff.
Getting that to work is unfortunately complicated, and you should just avoid
having classes without a specified package name.

Good luck!
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Doug Robinson
Guest
 
Posts: n/a
#3: Aug 15 '07

re: Need help linking & packages


You should just do:
Thing t = new Thing();
/* or whatever. If you haven't imported another.* or another.Thing

you can say another.Thing t = new another.Thing(); */

// Class.forName("Stuff");

This is an example of trying to use the unnamed package to look for
Stuff. Getting that to work is unfortunately complicated, and you should
just avoid having classes without a specified package name.

Thank you for your response!

I am well aware of all of the things you say. but but but...

The recomended way to "connect" to say a jdbc driver is indeed

Class.forName("driver.package...");

and in no way was I recommending the use of the "unnamed package" just
that I CAN "connect" to jars that contain one.

However I still can not make any reference to a "package" work in this
fashion.

Thanks again for your time & effort!

dkr
Mark Rafn
Guest
 
Posts: n/a
#4: Aug 15 '07

re: Need help linking & packages


Doug Robinson <dkrqXXX@XXXtelusplanet.netwrote:
Quote:
>Thank you for your response!
>I am well aware of all of the things you say. but but but...
Quote:
>The recomended way to "connect" to say a jdbc driver is indeed
>Class.forName("driver.package...");
Ah, yes. JDBC driver loading involves certain tricks where a driver registers
itself when the classloader first loads the class. Knowing you're having
troubles with JDBC rather than general java package questions is helpful.
Quote:
>However I still can not make any reference to a "package" work in this
>fashion.
Then it's time to get specific. Show us a small self-contained example, and
the output you get when you run it. A single class with a main(String[]) that
loads the driver should be sufficient.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Closed Thread