aspose file tools*
The moose likes Beginning Java and the fly likes how to access a class within a package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to access a class within a package?" Watch "how to access a class within a package?" New topic
Author

how to access a class within a package?

Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
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???


SCJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Welcome to JavaRanch

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.
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

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.

Have a look at Sun's Java Tutorials: Creating and Using Packages


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
ok the question i have been trying to solve is as follows :

1) Create the superclass as follows :
package food;
public abstract class Fruit{/* any code you want*/}

2) Create the subclass as follows in a different file:
import food.Fruit;
class Apple extends Fruit{/* any code you want*/}

3) Create a directory called 'food' off the directory in your class path setting

4) Compile and run

how do i go about it?
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
i forgot to mention that the Apple class isnt placed in any package...
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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.)
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
Faisal Ansari wrote:i forgot to mention that the Apple class isnt placed in any package...

? Perhaps you could post your Apple and Fruit source files.
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
thanks max i got it
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36


Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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.
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
thanks max....but how to set the classpath is only the thing i need to know....can you help me
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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 "\".)
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
thanks max....i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to access a class within a package?