Off the diretory D: i have created a package named 'practise' which contains a subpackage named 'food' inside which there is a class named as Fruit which is public..
Now i have created another class named Apple which is default and is present in the package 'practise' and extends Fruit...
how do i set the classpath, compile and run the program???
Don't set the classpath at all.
So you have?
Create a folder to hold your Java work and put all your work in that, rather than the root of the D drive. Otherwise you are liable to get your D drive cluttered and confused.
You need another class in the practise package which calls the Apple classPut your Fruit, Apple and AppleDemo .java files into your java folder. No need to create any other folders yet.There are several other ways to do it, but that method will work.
The -d option creates any directories required, and the . tells the javac tool to look in the current directory for the ".java" files. You can find other ways to do it by searching this forum, and this thread is one of the things you find. Beware: I made a mistake in that thread, and somebody else noticed it.
thanks for the welcome and speedy reply.....i tried what you said but unfortunately it didnt work out....can you elaborate a bit more or shed light on some other ways to the problem
Faisal Ansari wrote:i tried what you said but unfortunately it didnt work out....
Please explain what you did and why it didn't work, because just saying "it didn't work" is not very useful and doesn't help us to help you solve the problem.
Faisal, one thing to keep in mind is that a class's name is actually the fully qualified name -- i.e., the package and the class name. So your classes aren't simply Apple and Fruit, but practise.Apple and practise.food.Fruit. The location of those classes is the directory where that package structure starts. In your case, it's the root of your D drive. In other words, both practise.Apple and practise.food.Fruit are at at the same location. In this case, if you need to set classpath to point at your classes, you would point to D. If you were to code Apple on your C drive, C:\practise\Apple, and Fruit on your D drive, D:\practise\food\Fruit, then your classpath would specify two locations, D and C.
I'm just pointing this out because beginners often think of a class name as separate from its package name, which leads to confusion about imports and classpath. I'm also pointing it out to illustrate that classpath points to the directory of where your package.Class is defined, not to the actual file location of the class itself.
(Note that you would not normally split your package into two folders or directories, like I did above when I put Apple and Fruit on the C and D drives. It's allowed, but I only did that to illustrate how the classpath would be affected. In practice, separating your source code like that would be confusing. However, I do normally separate my jUnit tests into their own source folder, but that's another topic.)
Ah. I see that your classes are in package "food" and the default package. So your classpath would be set to whatever folder holds food.Fruit and Apple.
Note that most development environments help manage classpath for you. As a beginner, it may be best to do these things manually to better learn about what's going on. Soon, setting classpath becomes a hassle, and you'll probably want to switch to a more sophisticated development environment.
If you're running on Windows, then you can use Notepad and the command line for compiling and running your code. Or use Textpad, which is slightly more convenient, since you can run javac from within the editor, yet doesn't help too much with regard to classpath. Later you can switch to something like Eclipse.
From the command line, navigate to the folder where "Apple.java" is coded and type: javac Apple.java. This sets classpath to its default, and looks in the current folder for the classes Apple references.
If you were one folder up, and your code is in folder "practise", then you'd type: javac -classpath practice/ practice/Apple.java. In this case, -classpath says "look in the practice folder practise as the starting point to resolve class references".
(I'm in UNIX, so I use "/". If you're in Windows I guess you'd use "\".)