Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 14th, 2007, 04:35 PM
Doug Robinson
Guest
 
Posts: n/a
Default Need help linking & packages

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.
  #2  
Old August 14th, 2007, 08:05 PM
Mark Rafn
Guest
 
Posts: n/a
Default 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/>
  #3  
Old August 15th, 2007, 05:15 PM
Doug Robinson
Guest
 
Posts: n/a
Default 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
  #4  
Old August 15th, 2007, 06:35 PM
Mark Rafn
Guest
 
Posts: n/a
Default 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/>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles