File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath problem

 
Mubeen Shaik
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have created a class - ClassA (This class is not a main Class) in a directory C:\JAVA\Examples\Chapter10.
Now i have created one more class TestClass (This is main class - Please correct me if my terminology is wrong). This "TestClass" is invoking the method of ClassA.
I created a object of type ClassA and refered to a variable Var1.
ClassA Var1 = new ClassA();
When i compiled this i got an error ClassA Not found.Then i set my classpath to
.;C:\JAVA\Examples\Chapter10. Then it compiled normally.
I want to know the importance of Classpath and why it worked when i changed with "." and Directory name? Please explain me more about the Classpath.
Thanks in advance,
Mubeen Shaik.
 
David Peterson
author
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you compile your source code (which normally has a ".java" extension), the result is ".class" file which contains a compiled "bytecode" version.
Classes are compiled one by one. If the class you are developing depends on another class (e.g. TestClass refers to ClassA), the compiler must be able to access the relevant ".class" file for that other class.
The classpath tells the Java compiler where it can find these precompiled Java classes.
The ".", at the start of the classpath you mentioned, means: "search the current directory" first, then if no "ClassA.class" file can be found, try the next directory in the classpath ("C:\JAVA\Examples\Chapter10"). If it still can't find the class file, you'll get a "Class not found" error message.
Hope this helps.
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Peterson:
When you compile your source code (which normally has a ".java" extension), the result is ".class" file which contains a compiled "bytecode" version.
Classes are compiled one by one. If the class you are developing depends on another class (e.g. TestClass refers to ClassA), the compiler must be able to access the relevant ".class" file for that other class.
The classpath tells the Java compiler where it can find these precompiled Java classes.
The ".", at the start of the classpath you mentioned, means: "search the current directory" first, then if no "ClassA.class" file can be found, try the next directory in the classpath ("C:\JAVA\Examples\Chapter10"). If it still can't find the class file, you'll get a "Class not found" error message.
Hope this helps.

David,
Thanks for that detailed explanation.
Mubeen.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic