• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Importing all the classes from a single package

 
Ranch Hand
Posts: 897
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
I was wondering if you could clear up this issue I am having. I am using Suns JDK 1.4 on Linux (SuSE 7.3).
Say I have a number of classes (call them A and B) that are a part of package org.markfletcher.somepackage
The code for my classes might look something like this:

I compile these classes using the following command line:
javac -d ./ A.java
javac -d ./ B.java
My question is this: I now have a driver class called Driver that uses classes A and B from org.markfletcher.somepackage. On the platform I am using I find that the following code compiles fine:

However the following doesnt:

Id like the latter Driver.java code to work, because obviously as the number of classes in org.markfletcher.somepackage increase, it will save on typing!
Can anyone suggest what I am doing wrong?
Cheers,
Mark
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error are you receiving when attempting to compile the second example? The only reason I know the compiler would distinguish between the two imports is an ambiguous class name (occurs in >1 package).
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Mark,
I've tested your code and got the error. And the interesting stuff is if you add the package structure ("package org.markfletcher.somepackage") to the Driver.java file you won't have to even import the A & B classes. Everything works fine.
There may be bug in jdk1.4 compiler.
Look for posting "package/classpath problem with 1.4" under my name. I've already file a bug to SUN and they accepted it as a new bug in the compiler.
The problem which you r facing might have to do with the bug since you are trying to use classes where some classes has package structure and some do not.
-mike
 
Mark Fletcher
Ranch Hand
Posts: 897
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres the error made when trying to compile the Driver.java file, (FYI Driver.java is located in the same directory as org.markfletcher.somepackage package)
Driver.java:7: cannot resolve symbol
symbol : constructor A ()
location: class A
A myA = new A();
^
Driver.java:8: cannot resolve symbol
symbol : constructor B ()
location: class B
B myB = new B();
^
2 errors
My thinking is that my classpath may not be set to look at packages in the current directory?!?
Mark
[ March 12, 2002: Message edited by: Mark Fletcher ]
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Fletcher:
Heres the error made when trying to compile the Driver.java file, (FYI Driver.java is located in the same directory as org.markfletcher.somepackage package)


Ah. Don't do that.
When you compile FQP (fully-qualified package) code in the same directory as non-FQP classes, you're asking for trouble. This creates an ambiguity for the compiler, which cannot be "in" and "on top of" a package structure at the same time. You can help it along, but only with fully-qualified, compile-time references. (Incidentally, I don't see this particular problem as a bug, but if the J2SE1.4 engineers do, who am I?)
Here's an excerpt from a sleep-deprivation-induced interview with a Java compiler I conducted long ago: So I ask the guy, "Is this directory the 'anonymous' package space (current directory) or is it a named package space?" He says, "Uh, both?" "Ok," I say, "well I can do this, but the space I'm in when I'm in it, you have to make that unambiguous." Then, get this, he gives me some wildcard import statements. Nice. So what do I do? Well I supposed I could have been nice and told him to look up unambiguous in the dictionary, but I ain't his mother, and if he's playing like that, I don't wanna be. So he passes me a symbol, I says, "I don't think I know that symbol that by the way is sitting right in front of me. Sorry."
He didn't come around for a long time after that; serves him right."

Keep your namespaces clean, in short, and the compiler gets that much less cryptic.
[ March 12, 2002: Message edited by: Michael Ernest ]
 
Mark Fletcher
Ranch Hand
Posts: 897
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha!
Ive got it working, heres what I did.

Running the last line successfully runs the driver program
Now say I have a third class C, that users class B;

I get this to compile from the command line by typing

Thanks for the pointers Michael!
To Mike, can you try this on your PC and see if you get the same results?
Cheers,
Mark
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
{quote]Here's an excerpt from a sleep-deprivation-induced interview with a Java compiler I conducted long ago
LOL! Awesome way of explaining it Mike. If only there were text books that had that kind of sense of humour.
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd be surprised how negative some reactions are to it (I know I am). The technical publishing industry is largely focussed on fact, clear explanation and (of course) rapid sales. If humor has a place in that market, it's a mobile, fleeting one. For every one person I get who says they like a humorous take, I get more that say I'm "unprofessional," "not serious," "detracting from the point," etc.
It's fun to write them just the same. Maybe I'll work this topic into a Campfire story, since it's a long story to begin with and good for some fun.
[ March 12, 2002: Message edited by: Michael Ernest ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A prerequisite for a good sense of humor is intelligence. Stupid people don't find much funny. When people deride humor as unprofessional, they're usually graying middle-aged wannabes perpetually stuck in middle-management.
Then BEST computer book I ever read in my life was Macintosh Programming Secrets by Scott Knaster.
http://www.amazon.com/exec/obidos/ASIN/0201581345/qid%3D945381551/sr%3D1-1/002-6071481-3370442

It was funny and informative. It had a lot of technical information, some hard to process, so the humor took the edge off the learning.
I say, more books with humor are what this industry needs!
 
mike seluker
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Fletcher:
Aha!
Ive got it working, heres what I did.

Running the last line successfully runs the driver program
Thanks for the pointers Michael!
To Mike, can you try this on your PC and see if you get the same results?
Cheers,
Mark


Hey Mark,
I've checked your code and it worked. Also I know the reason why your first code type didn't work.
Here you have created "javalibs" directory and then compiled everything. If you remove "javalibs" directory it won't work. The reason is the presence of A.java and B.java in "." directory. Try this
Directory structure: A.java and B.java are present in /tmp
tmp> javac -d . A.java
tmp> javac -d . B.java
tmp> javac -classpath ./ Driver.java
Won't work.
Now do this.
tmp> mv A.java A1.java
tmp> mv B.java B1.java
tmp> javac -classpath ./ Driver.java
Works gr8. That means the presence of java file gives the problem.
What a weird thing.....
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/interpack/managingfiles.html
"By default, the compiler and the interpreter search the current directory and the ZIP file containing the Java platform class files. In other words, the current directory and the Java platform class files are automatically in your class path."
This was the best I could find, but I'm sure there's a better explanation of namespaces and packaging/importing elsewhere. The basic rule is that if a class is not fully qualified in the code when an object of that type it is created it will search the classpath and the import statements.
Just like Mike said right above, with a little modification, the problem was you had an additional A.java and B.java in your classpath in a different location than the packaged versions, which means they were in a different namepace than the A and B in the 'org.markfletcher.somepackage.*' you were importing. It therefore couldn't resolve which one you specifically wanted.
You should either remove A and B all locations except your package, or if they are actually different than the A and B in your package you should put them in their own package. Even so, if you need to use both in the same class you will have to call them out with fully qualified names. It's just like standing in a crowded public area and yelling "Hey, John!" and having half a dozen people turn to look at you. It works better if you try again - "Uh, I mean, 'Hey John Jacob Jingleheimer-Smith!" Java just has to have one and only one class resolve when more than one of the same name is around.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's my name, too!
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic