File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes classpath related query pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath related query pls help" Watch "classpath related query pls help" New topic
Author

classpath related query pls help

Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi,
Hey i would just want to clarify my doubt regarding the classpath variable...
whenever we have import java.mypackage.*;
we are importing the class files that are present in the package named mypackage then why do we have this classpath....which all class files does this one includes.
eg-:
package differentone;
import java.mypackage.*;
class A
{
A()
{
}

void createnewobj()
{
CheckPost chkpst=new CheckPost();//class exists in mypackage

}

public static void main(Strign a[])
{
new A().createnewobj();
}

}


Now to run this program i need a compiled version of checkpost which is present in the package named mypackage....so i have imported it..Now why does the classpath variable needs to be set if i import all the class files by the import keyword itself.
what i basically want is what happens by import and what happens by setting the classpath....ie if i set classpath to point to "mypackage" (in the above case) then is it ok if i do not import the package named mypackage in my prog.
Thanking everyone in anticipation.....
[ June 29, 2005: Message edited by: Archies Gomes ]
Steve McCann
Ranch Hand

Joined: Oct 20, 2004
Posts: 81
The classpath tells the compiler where to find the classes on your computer.
The import statements are nothing more than a shorthand way of specifying the class names.
e.g.

can be shortened with an import statement to

Useful if you are going to use the same class several times.

[ June 29, 2005: Message edited by: Steve McCann ]
[ June 29, 2005: Message edited by: Steve McCann ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Archies --

Although Steve implied this, he didn't say it explicitly: I bet you're expecting "import" to be similar to "include" in C/C++. It's not. It doesn't copy anything from any other class files: it just affects how you refer to other classes in your source code. In this regard it's actually very similar to "using namespace" in C++.


[Jess in Action][AskingGoodQuestions]
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi ,
So Ernest you mean that import is just for making the class visibility for the current source file ....then please tell me actually what all classes the classpath variable has to point to(besides class files required for the JVM ).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Every class file that's required for compilation, plus all the classes those classes need, etc. You don't need to worry about putting the java.* classes on the class path, or the javax.* classes from the core APIs (like javax.swing) because the JVM knows how to find those. But any other classes that you use do need to be available at run time, and the JVM needs to be able to find them -- and the class path is how it's done.

To again use a C/C++ analogy: classes or JAR files are like dynamic libraries (DLLs or .SOs.) You compile against them (or their header files) but then you have to link against them at runtime, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath related query pls help