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

package access trouble

 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have two folders

and



There are two source files in source
and class files will be put in classes


this is the directory structure




and both the source files are



and



Fruit.java has been compiled properly and the .class file is here classes/food/Fruit.class

but when I compile Apple.java with -classpath option



it gives me errors like these



I also tried to put food folder in classpath environment variable
and compiled this way


and it gave these errors



I'm doing something wrong, can someone please correct me?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch!

Fruit says it's in package food but it's not in folder food.

See if that helps. Here is a quick tutorial that includes building packages.
[ September 26, 2007: Message edited by: Stan James ]
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you only need "classes" folder in the classpath, not "food".
 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both Stan and Abhinav,

it is bening executed fine. But now I have another question,

when I change my Apple.java file by inserting only one sentance



the file will be,





everuthing else is the same as earlier. So now, I have put Apple.class file in the same food folder where Fruit.class file is there.

and then if I execute that Apple.class file it gives me these errors,



I did not understand this.
 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error I mentioned in my last post occured for below Apple.java also.



I thought "may be" import statement was causing the trouble since I'm putting both the .class files in the same folder. So I removed it.



But surprisingly, it compiled fine and created Apple.class file in the same food folder also but when I tried to execute that Apple.class file, it gave me the same error as I mentioned in my previous post.



I am so

for both cases, error message is same.
I was just trying to put all my .class files at one place but how am I supposed to do that, if such things happen. I am definetly missing out something or doing it wrong way.

[ September 26, 2007: Message edited by: Sarah Jorden ]
[ September 26, 2007: Message edited by: Sarah Jorden ]
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your classes are in package "food" and hence in folder classes\food, you should execute them from
the "classes" folder like this -

assuming your classpath includes the current folder.
 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Abhinav,

Thanks a lot. That helped.

It worked

One more thing, if I have one file A which doesn't contain any package statement. If I have some other file B which wants to import file A, then even if I give file A's absolute path [ e.g. project1.classes.part1.A ] with import statement, I can't access it.
For that I will have to put this statement in A.java file


Does that mean that if we want some file to be imported, we have to explicitly mention the package in which it is going to reside. We can not compile it first and then move those .class files at desired folder.

[ September 27, 2007: Message edited by: Sarah Jorden ]

[ September 27, 2007: Message edited by: Sarah Jorden ]
[ September 27, 2007: Message edited by: Sarah Jorden ]
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your class is in the default package (i.e. no package statement), it can't be imported. however, it doesn't mean it can't be used. all you need is to put this class in your classpath and use is directly.
 
Sarah Jorden
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Abhinav,

That was a great help. I appereciate it.
[ September 27, 2007: Message edited by: Sarah Jorden ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic