aspose file tools*
The moose likes Beginning Java and the fly likes package access trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package access trouble" Watch "package access trouble" New topic
Author

package access trouble

Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

you only need "classes" folder in the classpath, not "food".
Sarah Jorden
Greenhorn

Joined: Sep 26, 2007
Posts: 16
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

Joined: Sep 26, 2007
Posts: 16
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

Joined: Nov 19, 2002
Posts: 349

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

Joined: Sep 26, 2007
Posts: 16
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

Joined: Nov 19, 2002
Posts: 349

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

Joined: Sep 26, 2007
Posts: 16
Thank you Abhinav,

That was a great help. I appereciate it.
[ September 27, 2007: Message edited by: Sarah Jorden ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package access trouble