Hello.
When you reply to a message, please use this forum, not by personnal
message : you'll have more reactions.
How you said in the last message you send to me in my e-mail, I've
seen your problem and it has 2 options :
1. Since you use both Food and FoodMain classes in the same package
("fruitpackage" package is included in "foodpackage" package, it's why
you declared: "package fruitpackage" for FoodMain, and "package
foodpackage.fruitpackage;" in Food), you just have, i said, to declare
"package foodpackage.fruitpackage;" in top of FoodMain.java .
for this solution, you don't have to worry about "classpath" and
"namespaces".
2. The second solution use more complex comprehension of both the
classpath and the namespace, and on the other hand, the place (path)
from where you use the command line to execute your progam.
2.1. If don't you leave packages's declarations as in your source
files and modify them as I mentioned before (1.), there won't be a
problem, but following explanations may help you later;
2.2. If you change it, there is one important point you have to
undeerstand : creating a new package tells the JVM to create a new
namespace, and to not use the global one. read this for more details :
www.java.sun.com/docs/books/tutorial/java/package/managingfiles.html
The second point to understand is that, the place from where you use
the command line to compile is important, because the JVM will look
first from the current directory, and in the classpath to find your
source files. If you declare in a source file a package, the JVM will
directly look in the folder relative to your package, from the current
path.
Ex: Assuming that the command line prompt is : "c:\foodpackage
\fruitpackage>" as in your case. Then, the current path for the JVM is
"c:\foodpackage\fruitpackage".
When you type c:\foodpackage\fruitpackage>javac Food.java
FoodMain.java , the JVM will search the files in the current path and
will set (for the time) the classpath to it for the 2 files.
--> in FoodMain.java, you have "package fruitpackage;" and in
Food.java, you have "package foodpackage.fruitpackage;"
--> the JVM will check if there is a namespace corresponding to
these paths, and it will be ok because you have created them.
--> but when compiling, there is no problem with Food class because
it doesn't require or call another class for his execution.
For FoodMain class, there is a problem because it calls the Food
class in his execution.
Now, looking for package's declarations, from the current namespace
and path (relative to FoodMain which is "c:\foodpackage\fruitpackage")
the JVM will search FoodMain.java in the package "fruitpackage" which
should be located at "c:\fruitpackage". For Food.java, the JVM will
search it in the package "foodpackage.fruitpackage" located at "c:
\foodpackage\fruitpackage".
As you can see, the 2 files aren't in the same directory, and the
current directory for the JVM when trying to execute FoodMain, is "c:
\fruitpackage". The JVM will then search Food class in that directory
or in the Global namespace, or in the folder ... "c:\fruitpackage
\foodpackage\fruitpackage" ! Yes, it's not a mistake : "c:\fruitpackage
\foodpackage\fruitpackage".
For these reasons, because the 2 files aren't in the same package,
there are in different namespaces, and because FoodMain calls Food,
you need to include an "import" statement in FoodMain as "import
foodpackage.fruitpackage.Food;" after package declaration (after
"package fruitpackage;"). Of course, don't forget to save Food.java in
"c:\foodpackage\fruitpackage" and FoodMain.java in "c:\fruitpackage".
@+
On 28 fév, 13:43, belmars <[email protected]> wrote:
> Just to rectify/add something :
>
> You declared
>
> --> package fruitpackage;
> public class FoodMain
>
> --> package foodpackage.fruitpackage;
> public class Food
>
> So, you have FoodMain.class and FoodMain.java in the folder
> your_workspace\fruitpackage\ , and Food.class and Food.java in the
> folder your_workspace\foodpackage\fruitpackage. Apply it to what I
> said before.
> (I think command "c:\foodpackage\fruitpackage>javac Food.java ..
> \FoodMain.java" from within "c:\foodpackage\fruitpackage" folder will
> work).
>
> @+
>
> On 28 fév, 13:14, belmars <[email protected]> wrote:
>
> > Hello.
>
> > First, you have to keep somthing in mind : when working in your IDE,
> > and declare classes and packages, they are stored in your workspace
> > (the folder in which you choose as your "root" folder to store
> > projects : I'll call it your_workspace).
>
> > Then, when you declare a new package, it creates a new "namespace"
> > for relatives files, and will store them in the folder your_workspace/
> > your_package_name/ (assuming that you're working in linux. if not,
> > replace / by \ in the folder's path).
> > Thus, if you create a new class as Food.class or FoodMain.class (by
> > creating and compiling Food.java and FoodMain.java) in your package
> > (by declaring the "package" statement in their declaration), you have
> > to look them in the folder your_workspace/your_package_name.
>
> > Now, looking for your problem, it seems that you haven't declared
> > any "package" statement in the FoodMain.java. But when compiling in
> > command line, you used the command "c:\foodpackage\fruitpackage>javac
> > Food.java FoodMain.java" which indicates that you are in the folder
> > relative to "fruitpackage" package : FoodMain.java is not here, you
> > have to look it in the folder c:\foodpackage, or in the folder
> > your_workspace.
> > For compiling, try this command : c:\foodpackage>FoodMain.java from
> > within "c:\foodpackage" folder, or "c:\foodpackage\fruitpackage>javac
> > Food.java ..\FoodMain.java" from within "c:\foodpackage\fruitpackage"
> > folder.
>
> > @+
>
> > On 28 fév, 11:10, Rafał Laczek <[email protected]> wrote:
>
> > > Hello Friends,
>
> > > I have a problem to create file FoodMain.class
> > > I used all suitable proposed in lesson commands. For example using
> > > following one:
> > > c:\foodpackage\fruitpackage>javac Food.java FoodMain.java
> > > system created Food.class in fruitpackage but regarding FoodMain I
> > > receive communicate:
> > > FoodMain.java: cannot find
> > > symbol: class Food
> > > location: class fruitpackage.FoodMain
> > > Food appleFood = new Food(); //this is one of the objects.
>
> > > The tops of classes in java files are following:
> > > package fruitpackage;
> > > public class FoodMain
>
> > > package foodpackage.fruitpackage;
> > > public class Food
>
> > > The cod of classes is OK because project works in IDE.
>
> > > I enclose print-screen.
>
> > > I am a beginner in Java.
>
> > > Thank you very much for your help!
>
> > > Best regards,
> > > Rafał
>
> > > ----------------------------------------------------
> > > ATRAKCYJNE NIERUCHOMOŚCI W ZAKOPANEM !!!
> > > Apartamenty, Domy, Działki, Pensjonaty, Hotele, Lokale użytkowe...
> > > Kliknij:http://klik.wp.pl/?adr=www.nieruchomosciwzakopanem.pl&sid=652
>
> > > lab1003.doc
> > > 39KAfficherTélécharger
>
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---