• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another Error: Could not find or load main class question  RSS feed

 
Jimbob Greaves
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been trying to run a simple Hello World program from the command prompt but each time I run "java Example" from the directory where it is located I get the error "Error: Could not find or load main class". so I copied some HelloWorld code from this site just to test, compiled, and ran from the same directory, again: "Error: Could not find or load main class". I'm not entirely sure what's happening, all the tutorials I have seen say running Java from the directory of the .class file should work but nope, not for me. I have been struggling for over an hour to get this running, looking into CLASSPATHs and Java paths, I've tried setting my CLASSPATH environment variable (in Advanced System Settings AND in command prompt), but that seems to do absolutely nothing (even an "echo %CLASSPATH%" prompt returns "%CLASSPATH%"), I've now deleted the CLASSPATH variable as it's a silly thing to use. I've tried politely showing java the path I want it to use, nothing. I can't quite understand why it's so difficult just to get a little program to run in command prompt.

I've included the code below, there's nothing wrong with it, it compiles, and runs fine testing it in Eclipse.

Is Eclipse the issue? Has it messed around with something I don't know about?




I hope this isn't a repeat post, I did a search on this forum, but nothing seemed similar, even though a few of the sites I have looked at say this is a common issue for noobs.

Any help anyone could provide would be greatly appreciated.
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look for this recent similar thread. Please get back in case you won't manage to solve issues.

And welcome to the Ranch
 
Jimbob Greaves
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using "java -cp . Example" as suggested in the post you pointed me to worked! Thanks a lot!

Not sure why that post didn't appear in my results, but hey ho, I found the solution in the end.

Could someone explain why the previous method didn't work from me? I have no CLASSPATH that I can find, so surely java.exe should just use whatever path it is called from, but it seems to behave like a CLASSPATH is set somewhere even if I can't find it. And maybe if there's a way to fix it for the future?

Thanks for the welcome, I hope to become a regular in these parts, all you actual regulars get ready for many more facepalm questions!
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Have you tried the following at a terminal/command prompt?
echo %CLASSPATH%
(Windows®) or on Mac/Unix/Linux
echo $CLASSPATH
The ideal response is a blank line. There are some programs which set the CLASSPATH while you are not watching; QuickTime used to notorious for doing that. Oracle, too. I don't know whether that still occurs.
 
Jimbob Greaves
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did, thank you, but the only response I ever seem to get from that command is "%CLASSPATH%" even when I have manually set a CLASSPATH in environment variables.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimbob Greaves wrote:I did, thank you, but the only response I ever seem to get from that command is "%CLASSPATH%" even when I have manually set a CLASSPATH in environment variables.

That's usually good -- that is, getting %CLASSPATH% back means it's not set, which is what you want in normal circumstances.

I suspect that the reason you didn't see your manually set CLASSPATH is because you have to restart the command window afterwards.

Using the "-cp" flag with java is a much better way to handle classpath needs.
 
Jimbob Greaves
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. The "echo %CLASSPATH%" command didn't return the correct value even after a system reboot. Maybe the environment variable was entered incorrectly or something, no matter, I've deleted it now, and using the command in the suggested post works fine for me so I'm not too worried, I think I'll be using Eclipse for most of my Java programing anyways. It's just worrisome when my computer doesn't behave the way it's supposed to.

Thanks again to all the guys that gave me advice. This forum looks like it'll steadily become my go to site for Java!
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimbob Greaves wrote:. . . Thanks again . . .
I am sure deleting the CLASSPATH was the best solution. As Knute said, use the -cp flag if you need a specific CLASSPATH.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!