0

I am trying to run a java file in the cmd prompt in Windows 7.

I get the error:

Error: Could not find or load main class 

I actually just save a new simple file to check if there were problems with the package inside eclipse; this new file is saved just as:

C:\Users\User5\Documents\eclipse\test\Example.java

class Example {
    // A Java program begins with a call to main().
    public static void main(String args[]) {
    System.out.println("Test.");
    }
}

I changed the classpath for lucene's jar's recently, and I am not really sure if this is the problem.

There are many other threads about this issue, such as:

Could not find or load main class

but, there seem to be other concerns that solved their issues.

In this case, I have saved just a plain file in notepad, and while I can get the file to compile, and it seems to create the class file, it is still spitting this error back.

This is the dir, which seems to show that the class is there:

C:\Users\User5\Documents\eclipse\test>dir
 Volume in drive C has no label.
 Volume Serial Number is 3E0D-3B82
 Directory of C:\Users\User5\Documents\eclipse\test

12/07/2015  10:15 AM    <DIR>          .
12/07/2015  10:15 AM    <DIR>          ..
12/07/2015  10:04 AM               301 .classpath
12/07/2015  10:04 AM               380 .project
12/07/2015  10:04 AM    <DIR>          .settings
12/07/2015  10:05 AM    <DIR>          bin
12/07/2015  10:51 AM               428 Example.class
12/07/2015  10:15 AM               162 Example.java
12/07/2015  10:05 AM    <DIR>          src
               4 File(s)          1,271 bytes
               5 Dir(s)  10,000,461,824 bytes free

C:\Users\User5\Documents\eclipse\test>java Example.java
Error: Could not find or load main class Example.java
Community
  • 1
  • 1
JerrittPace
  • 21
  • 1
  • 8
  • Yes, because the class isn't called `Example.java`, it's called `Example`. You specify the name of a class, not the name of a file. This is bound to be a duplicate - will see if I can find one... – Jon Skeet Dec 07 '15 at 16:11

5 Answers5

1

This should to the trick - don't append .java

C:\Users\User5\Documents\eclipse\test>java Example
Jan
  • 13,738
  • 3
  • 30
  • 55
1

I think I need to make this in a larger post:

C:\Users\User5\Documents\java\test>java Example Error: Could not find or load main class Example

I just accidentally posted the wrong copy to the original post here, but the file is not running as just a call to the file. This is not the only file I cannot get running; nothing will run in the cmd prompt, although everthing runs fine in eclipse.

Thank you'all for your help, I really appreciate you!!

JerrittPace
  • 21
  • 1
  • 8
0

C:\Users\User5\Documents\eclipse\test>java Example.java

will not run - take out the .java part and simply run

C:\Users\User5\Documents\eclipse\test>java Example

To make sure you do not have an issue with your CLASSPATH variable, do

set CLASSPATH=
java Example

If that is the issue, your classpath variable gets corrupted, you can go to the Control Panel, somewhere in the advanced section to update environment variables.

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Yeah, that was actually a mistake putting that line there, the actual line was: C:\Users\User5\Documents\java\test>java Example Error: Could not find or load main class Example – JerrittPace Dec 07 '15 at 16:29
  • I just was at a point that I was trying anything, and I accidentally copied the wrong printout, but the Example file is not running...This is not the only file that I cannot get it to run, it's just the simpliest example that I wanted to use to make sure that I had tried to fix the problem if it was something else. I don't know what I have done, but I have altered something and cannot get anything to run in the cmd prompt any longer, although everything runs fine in eclipse – JerrittPace Dec 07 '15 at 16:32
  • add a simple test to make sure classpath is not corrupted – Frederic Henri Dec 08 '15 at 08:18
0

I got the same error when I needed to take inputs(arguements) at runtime using terminal/Command prompt and corrected it it by compiling and running the java class as below:

  • javac packageName/Example.java
  • java packageName/Example

NOTE: The reason for this error in my case was that the java class was created inside a package and that required compilation and execution in a different way as above mentioned.

dikoder
  • 1
  • 2
0

The reason could be any ... try resolving this. enter image description here

Zia Khan
  • 188
  • 2
  • 9