-3

When I try to compile any Java program using the command prompt, the class file is successfully formed and compile time errors, if any are also displayed. However, when I run the program using the command:

java ProgramName

then the following error is displayed:

Could not find or load main class ProgramName

This happens for all the Java programs I have in my system. I have already checked that the class path and the environment variables are set. I am still unable to find the bug here.

Pshemo
  • 122,468
  • 25
  • 185
  • 269

3 Answers3

0

Check for the main method public static void main (String[] args) inside your class.

drgPP
  • 926
  • 2
  • 7
  • 22
  • 2
    Use comments to ask for more informations. – Pshemo Mar 31 '15 at 10:14
  • @Pshemo, it's not an ask for aditional information but the answer for his problem... – drgPP Mar 31 '15 at 10:15
  • Are you 100% sure that this is **the** problem OP is having? – Pshemo Mar 31 '15 at 10:16
  • @Pshemo, not for 100% but i think for 70% for sure. However there are a lot of answers on stackoverflow which has no correct answers, but nobody gives downvotes for them.. btw i get your point why you have downvoted. – drgPP Mar 31 '15 at 10:17
  • Then IMO it should be comment. – Pshemo Mar 31 '15 at 10:18
  • @Pshemo actually seems a quite reasonable answer... maybe he should not ask, but propose to check.... – Jordi Castilla Mar 31 '15 at 10:19
  • 1
    @Jordi Castilla, yes, it's more as a propose, should have not post this answer as a question. – drgPP Mar 31 '15 at 10:20
  • @JordiCastilla if someone is not sure if solution is correct then proper action would be asking OP for more informations rather then posting it as answer. Also lack of `mian` method would cause different error which drgPP could easily check before posting an answer... This solution/propose is not correct. – Pshemo Mar 31 '15 at 10:28
  • @JordiCastilla Yes, I've seen this edit, it was just formatting of already posted informations. Also question was already properly closed so no need to vote there until OP will decide to include some more informations. But posting untested answer decreases credibility of Stack Overflow and I am against it. Also as mentioned this answer is wrong, since lack of main method will cause different error than from OP question. BTW you should probably read http://meta.stackexchange.com/q/74666/186652 – Pshemo Mar 31 '15 at 10:41
  • @JordiCastilla Lack of `main` method would result in `Error: Main method not found in class ProgramName, please define the main method as: public static void main(String[] args)` not `Could not find or load main class ProgramName` (notice the difference `Main method` and `main class`). OP problem is explained here: http://stackoverflow.com/q/18093928/1393766 and means that JVM couldn't find `.class` file which OP is trying to run. It has absolutely nothing to do with lack of `public static void main (String[] args)`. – Pshemo Mar 31 '15 at 10:53
  • When I'm wrong I'm wrong... @Pshemo , thanks for the links and explanation... ;) – Jordi Castilla Mar 31 '15 at 12:11
0

Most probably you are running like

java FileName

but you should run it with class name like

java ClassName

Also check the thread.

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65
0

you can try it with using -cp command which including class path. With the -cp argument you provide the classpath i.e. path(s) to additional classes or libraries that your program may require when being compiled

java -cp className
Pratik
  • 944
  • 4
  • 20