"Error: could not find or load the main class My.class"

Im using Java SDK 1.7 for Windows 7 via cmd.exe. Until a few hours ago everything worked correctly, all of a sudden I was not able to run my compiled class files, consistently presented with an error in the header.

It seems I can compile my My.java file, but I cannot run the resulting class file (My.class). I constantly get the error "Error: Could not find or load the main class My.class". I tried this with several other class files, all this leads to the same problem.

The environment variable "Path" is set to "C: \ Program Files (x86) \ Java \ jdk1.7.0_05 \ bin" if you are interested

I tried reinstalling, creating and setting the classpath variable (no luck) and even using directly

java -cp . My.class

teams.

I tried these posts all to no avail, so why am I posting:

Error: could not find or load the main class

Error: could not find or load the main class - newbie

Could not find or load main class

Java 1.7.0_03 Error: could not find or load the main class

If that matters, my code is:

 import javax.swing.JOptionPane; class My { public static void main(String[] args) { final double x = 3.2; int i = (int)x; double m = 0; if (x < 4) { String saySomething = JOptionPane.showInputDialog(i); System.out.println(saySomething); } else { String saySomething = JOptionPane.showInputDialog(i); System.out.println("Hello World"); } while (m < 10) { System.out.print(" While Loop "); m++; }; for (i=1; i < 10; i++) { System.out.println("For Loop"); }; } } 
+4
source share
4 answers

You must specify the class name instead of the loadable class file. The difference lies in the simple removal of the .class extension.

+12
source

I would use an IDE and you should not get these problems. Compiling and running is just a click away.

BTW to run your program from the command line

 java -cp . My 

You do not add .class

+2
source

Put yourself in the directory of your project (you need to have the src and bin directories, assuming you keep the sources in src and binaries in the trash)

 java -cp bin My 
+2
source

I myself faced the same problem. This happened because I was mistaken in typing the class name correctly. In my case, I typed

java doubler

instead

java doubler

0
source

All Articles