Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package problem

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shilpa kulkarni
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One quick question. If a superclass in a package, the subclasses don't automatically inherit the package, do they?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic