wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package access problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package access problem." Watch "Package access problem." New topic
Author

Package access problem.

Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
Hi all,

I have created the below file, and when I try to compile them I am getting errors. Can anyone tell me what is the problem with this code??

These three files are in a folder named Food and While compiling I had set my current directory to Food. I compiled with "javac Fruit.java"
It is giving me 6 errors in the places I have used Apple and Orange, saying "Cannot find symbol".

However If I run the same program in Eclipse it is giving me the correct output. Someone plaese help me to figure out what is going wrong here.
Thanks for your help.
[ January 31, 2008: Message edited by: Maya Raj ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Without an explicit CLASSPATH, Sun's default is to look in the current directory. Your current directory is Food, but Food.Apple and Food.Orange are not in that directory. They are in the parent directory of Food.

The preferred way to compile this would be to set the current directory to the parent directory of Food, then compile with...

>javac Food/Fruit.java

That way, Food.Apple and Food.Orange will be in the current directory.

As an alternative, if you must use Food as the current directory, then you can specify the classpath using two dots to indicate the parent directory...

>javac -cp .. Fruit.java


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
Hi Marc..
Thanks a lot for your reply.
But, I still have problem complilng above code. I tried both of your suggestions,
>javac Food/Fruit.java
>javac -cp .. Fruit.java

Now I am getting error message like cannot read food/Fruit.java and
cannot read Fruit.java
PLease see my direcrory structure below. I have 3 .java files in the Directory called Food, which is in MyJavaPrograms Directory. ie,


I also tried using absolute class path, in my case C:\Maya\JavaLearning\MyJavaPrograms\Food even this did not work. It gave me the same error. Can you please help me with this..

Thanks in advance.
ramchandra sugasi
Greenhorn

Joined: Jan 16, 2008
Posts: 10
Hi,

Please try the following:
I guess you are using the command prompt to do the execution.
In that case,
after you enter into the command prompt, go to the folder 'Food'
there, you need to type as follows:
>javac *.java
>java Fruit

you should be able to execute the program now.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It looks like you need to first change to the proper directory, which I might not have been clear about.

One way is to use the parent directory (MyJavaPrograms) as the current directory...

C:\>cd Maya\JavaLearning\MyJavaPrograms
C:\Maya\JavaLearning\MyJavaPrograms>javac Food/Fruit.java

Another way would be to use Food as the current directory...

C:\>cd Maya\JavaLearning\MyJavaPrograms\Food
C:\Maya\JavaLearning\MyJavaPrograms\Food>javac -cp .. Fruit.java

Remember: Because Apple and Orange are in the Food package, the location of Food.Apple and Food.Orange is not Food. It is the parent of Food, which is MyJavaPrograms. So if you are specifying an absolute path, it would not include the Food directory. It would just be C:\Maya\JavaLearning\MyJavaPrograms.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ramchandra sugasi:
...go to the folder 'Food'
there, you need to type as follows:
>javac *.java
>java Fruit

you should be able to execute the program now.

This will work to compile the .java files in the Food folder, but it will not work to run Fruit. The Fruit class is also part of the Food package, so the qualified name is Food.Fruit, which is in the parent directory.
ramchandra sugasi
Greenhorn

Joined: Jan 16, 2008
Posts: 10
ya,

i completed forgot that.
Thanks.
so inorder to compile,

>javac Food/*.java

to run,

>java Food.java

All these need to be done from outside the folder 'Food'.
ramchandra sugasi
Greenhorn

Joined: Jan 16, 2008
Posts: 10
ya,

i completed forgot that.
Thanks.
so inorder to compile,

>javac Food/*.java

to run,

>java Food.Fruit

all these need to be done from outside the folder 'Food'.
Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
Marc, Thanks again.. Now I got it..and I am able to compile and run the program..
Thanks Ramachandra, your suggestion worked too..

Can you suggest me some study material for classpath, java,javac commands for further study. I thik I need more practice on these topics..

Thanks..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Maya Raj:
... Can you suggest me some study material for classpath...

Chapter 5 from Eckel's 4th edition of Thinking in Java is good.
Maya Raj
Greenhorn

Joined: Nov 22, 2007
Posts: 12
Thanks Marc,
That helps me a lot..
 
wood burning stoves
 
subject: Package access problem.
 
Similar Threads
Excercise 1-1 from Kathy Sierra, Bert Bates Study Guide
method in subclass not seeing method in abstract superclass
Constructor of parent abstrat class not getting executed.
package access trouble