• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package access problem.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Maya Raj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc,
That helps me a lot..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic