aspose file tools*
The moose likes Beginning Java and the fly likes package problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package problem" Watch "package problem" New topic
Author

package problem

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I have created four classes, named Fruit (superclass), Apple, Orange, and Banana (all subclasses of Fruit, go figure). I put the line package com.workcomp.fruits ; in their code, and when compiling the class files go right to the d:\java\com\workcomp\fruits\ folder like expected.
However, when I try and compile the TestFruit class, which creates an array of random Fruit objects, it says that it can not resolve symbol when doing a new Orange(), new Apple(), or new Banana(). I have import com.workcomp.fruits.* ; in the code.
This looks like a path problem, of course. My classpath does have D:\Java in it, though.
Anyone have any ideas, and suggestions for someone who just recently started to do a bit with packages and .jar files?
Thanks!
Jason
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
If your classes are in d:\java\com\workcomp\fruits\ then your classpath needs to contain d:\java\com\workcomp\fruits\ so that the system knows where to find the classes.


"JavaRanch, where the deer and the Certified play" - David O'Meara
shilpa kulkarni
Ranch Hand

Joined: Jun 07, 2000
Posts: 87
i think, all u need is d:\java in your classpath. after that the import statement will look for the appropriate folders for the class file, namely the com folder wherein it will look for the rest of the path as mentioned in the import statement.
where is the TestFruit class? it sounds like it should be in the d:\java folder.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Cindy, I thought the whole point of packages was so that you can zip or jar them up and have them in a folder that is in your classpath, and the JVM knows what to do from there. From my experience with other programs, if I put the classpath for com\workcomp\fruits in my classpath, I would get some error like com\workcomp\fruits\com\workcomp\fruits\Apple.class can not be found (don't know if that is the exact error, but something along those lines). I'll give it a try, it just seems to me like I shouldn't have to.
Shilpa, yes, TestFruit.class is in D:\Java, which is already in my classpath.
Thanks y'all, I'll let you know what comes up,
Jason
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
One quick question. If a superclass in a package, the subclasses don't automatically inherit the package, do they?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
No, the subclass does not automatically inherit the package. You have to import the package. If you don't, you'll get a compile error.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Heh, found that one out just now, which is what I figured would happen, but since I haven't really played with packages much, wasn't sure.
I did what Cindy suggested, but now I'm getting an error stating:
TestFruit.java:10: cannot access Fruit
bad class file: d:\java\com\workcomp\fruits\Fruit.class
class file contains wrong class: com.workcomp.fruits.Fruit
Please remove or make sure it appears in the correct subdirectory of the classpath.
Fruit f ;
^
1 error
That's kind of what I was mentioning earlier, it's almost as if putting the whole path in the classpath is doubling the effort here. Seen this one before?
Jason
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
(Nevermind, the error was I had the classpath set to directory name including the package name. i needed it set to the directory just above the package name, ommitting the package name from the classpath)
The previous question was never answered and now I have the same type error where a test class is using a hierarchy of classes all in the same package. I've tried the test class itself in the same package and outside of it. When compiling, I get

employee and its subclasses appear in c:\javasrc\employeeStuff and they all have the package name employeeStuff.
I set classpath as follows:
set classpath=c:\javasrc\employeeStuff

the code for the TestEmployee is

[ June 07, 2002: Message edited by: herb slocomb ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Assume you have class MyClass in package com.mypackage. The .class file for MyClass would be in a directory such as:
c:\java\classes\com\mypackage
The classpath would contain c:\java\classes.
So for your problem:
Set classpath as follows:
set classpath=c:\javasrc\


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package problem