This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Error: Could not find or load main class packageb.ClassB in 1st chapter of Jeanne's OCA book  RSS feed

 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run below code using

 java packageb.ClassB  

from C:\Users\vilas it gives "Error: Could not find or load main class packageb.ClassB"

ClassA is in C:\Users\vilas\packagea  and ClassB is in C:\Users\vilas\packageb



I followed the directions in the book OCA by Jeanne.Please let me know if I am missing to understand something.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set the classpath for the JVM to findout where the class is present. Classpath is to tell JVM in which all paths, JVM has to look for the classes which are being used in the program.
The classpath should be set upto the directory where the packages are present. In your case, the classpath must be set to C:\Users\vilas. To set the classpath, the following command should be written on the commandprompt

set classpath=C:\Users\vilas

After you set the classpath, the code will execute perfectly.
 
Marshal
Posts: 63485
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neelam Agarwal wrote:You need to set the classpath for the JVM to findout where the class is present. . . . .

I don't think that is the actual problem, I am afraid. VP is already in that folder, so the default CLASSPATH will point to that folder.
VP: Please show us the contents of that folder. Did the classes compile correctly? Where is the ClassB.class file? Also tell us what happens when you try
I recommend you create yourself a folder for your Java® work:-You only need line 1 once, but use line 2 every time you go to yoru Java® work.
 
Neelam Agarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Neelam Agarwal wrote:You need to set the classpath for the JVM to findout where the class is present. . . . .

I don't think that is the actual problem, I am afraid. VP is already in that folder, so the default CLASSPATH will point to that folder.
VP: Please show us the contents of that folder. Did the classes compile correctly? Where is the ClassB.class file? Also tell us what happens when you try
I recommend you create yourself a folder for your Java® work:-You only need line 1 once, but use line 2 every time you go to yoru Java® work.



It can be the case here that the class is not compiled properly and it is not available in the package specified. I am afraid there is anything called as default Classpath in Java. Classpath must be explicitly set whenever we are talking about packages and we need to access the class inside a package.
First try finding out whether the classes are available and if not, compile the code and check for the classpath. If the classpath doesnot include the current path, include it and then try executing the code again.
 
Campbell Ritchie
Marshal
Posts: 63485
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If no CLASSPATH is set, the JVM defaults to using “.” as its CLASSPATH.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!