File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath" Watch "classpath" New topic


Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
hi all,

I found difficulties when I learn about classpath, it's really confusing. please find below example from my self code.

My directory structure as per below :

After several attempts, I have compiled and running above code and it was running well, here is the code :


running Horse.class

My question is why I have to specify the Animal class when I compiled and run the Horse? why I can't do it like this--> C:\Documents and Settings\Mila\Desktop\project> java -cp .\com\path;. Horse (no Animal added, only the classpath for animal??)

So confusing, really appreciate for your reply.

Swapnil Sonawane
Ranch Hand

Joined: Jan 02, 2008
Posts: 190
Because your Horse class extends your Animal class. It has its own class file. Am I clear?

Swapnil S. Sonawane<br /> <br />B.Tech (Expected May 2009)<br />Computer Science, NIT Durgapur, India<br />SCJA 1.0
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
Hi Swapnil,

thank you for your reply, but I'm still not clear about this. regarding this thread, it not necessary to mention the parentclass when using the classpath, or am I wrong or missing something??
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

the reason why you need to specify the base class in the path to run the sub class is because both of them are in different packages.
As in the example above
Animal class is in the package com.path while the Horse class is in the default package.

If both the classes are declared in the same package, then we need not add the base class in the path.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

This question isn't really about the SCJP exam, so I am moving it to the Java in General (Beginner) forum.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Fischer

Joined: May 02, 2008
Posts: 25
Does not mean that you will not see something like this on the certification exam. Things like this was on the Java 6 exam, at least.

I agree. Here's the link:
subject: classpath
It's not a secret anymore!