Hi Amit

I never thought it were a problem in the way you implemented the classes.

It is just that there is a classical schema for compiling java classes in packages that it is puzzling the first time you encounter it.

There are several things to notice:
-- When you compile a Java class, all the referenced classes must be found by the compiler, either in the ".java" format or in the ".class" format (i.e. compiled or not).
-- That is why the "javac" compiler needs a classpath too.
-- The current directory (i.e. the one designed by ".") is by default in the classpath. So if you have several interdependent classes with no package, you can just:

   *cd $MYDIR *(to the directory that contains the .java classes)
   *javac MyFirst.java
   javac MySecond.java*
   ... and so on ...

                    or (of course):

   *cd $MYDIR *(to the directory that contains the .java classes)
   *javac *.java*

-- If a Java class contains a "package" information, then it is supposed to be compiled and executed in a directory matching the package name.

For example, if the class "A.java" contains the declaration "package p" and the class "B.java" contains the declaration "package p.cls" and the two ".java" files are in the directory "$MYDIR" then you shoud:

   *cd $MYDIR *(to the directory that contains the .java classes)
   *mkdir ./p *(create the directory corresponding to the package "p")
   *mkdir ./p/cls***(create the directory corresponding to the package
   "p.cls")
   *mv ./A.java ./p/A.java***(put the class in the proper directory)
   *mv ./B.java ./p/cls/B.java***(... same thing ...)
   *javac p/A.java***(compile A)
   *javac p/cls/B.java***(compile B)
*java p.cls.B***(execute main in "./p/cls/B.class")
Of course:
-- A must call the methods in "p.cls.B" (or have an "import p.cls.B" or an "import p.cls.*") and not directly in "B"
--  and B must call the methods in "p.A" (or ...) and not directly in "A".

Hope it helps
Mihai

Le 23/03/2011 10:27, Amit a écrit :
Hello Mihai,

Thanks a lot for replying to the post.

I finally worked around it. There was no problem in the class and the
way it was implemented. Everything was clearly instantiated and
referenced.

The main culprit was how I was compiling it. I was able to compile the
Food class, but not FoodMain class as it was looking for Food class. I
was able to compile and generate the .class file, by compiling both
the files together bu using "javac *.java".


But again after compiling I was having trouble running the .class file
- its gave ClassDefNotFoundError, which I guess was because of the
classpath problem. As I was compiling it from the subdirectory where
it resided.I managed to run it flawlessly by going to the root folder
(Linux) and then running with the classpath option, as here : "java -
classpath<folder-where-package-is-present>  <package-structure>".

Thanks again for replying to my post, and I gave the explanation so as
it can help other people having such problem.

With regards

Amit.

On Mar 23, 12:39 am, Mihai DINCA<mihai.di...@free.fr>  wrote:
Hi Amit,

There must be some mismatch in the way the class Food.java is used
inside FoodMain and where the files are physically located. Can you send
the code of FoodMain and explain in which directory are located the files?

I mean, in the DOS window in which you type "javac FoodMain.java", what
is the current directory and in which sub-directory are located your
.java files?

Hope it helps
Mihai

Le 22/03/2011 16:47, Amit a crit :







I dont understnad what the problem is.
In order to check my logic, I recreated the same scenario in Netbeans
and it works perfectly. Can anyone please suggest something?
I finally managed to compile my FoodMain.java by compiling both the
files simulataniously with "javac *.java", but when I try and run
FoodMain - its gives me "NoClassDefFoundError". What do you think the
problem is?
Thanks.
On Mar 22, 5:27 pm, Amit<amitah...@gmail.com>    wrote:
Hello,
I am doing the following homework exercises which asks us to create
two .java files in package foodpackage.fruitpackage. The files are
Food.java and FoodMain.java.
I created and complied Food.java and when I am doing the same for
FoodMain.java and try to access Food class from it says symbol not
found.
I am pretty sure that I have done everything correctly. And in any
case they are in the same package, so Food class should be available
to FoodMain class. Both the files are in fruitpackage directory, then
why doesnt it work? Can anyone help me on it?
Thanks
With regards.

--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to