Could not find or load main class - for any program

could not find or load main class jar
error: could not find or load main class eclipse
error: could not find or load main class command prompt
could not find or load main class in cmd
error: could not find or load main class eclipse maven
could not find or load main class netbeans
error: could not find or load main class linux
error: could not find or load main class solution hackerrank

I'm having a problem where the java command - no matter what I'm trying to run, says that it Could not find or load main class.

Everything is fine when compiling with javac, .class files are created. So when I run:

javac HelloWorld.java

on

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}

everything compiles fine, a HelloWorld.class file is created along side the HelloWorld.java file. However when I then go to run:

java HelloWorld

1) the most telling sign is that when I press Tab to autofill HelloWorld nothing comes up.

2) when I do run it, I get the Error: Could not find or load main class HelloWorld despite it being in the same directory, not being part of a package, compiling fine with a .class file, the program having a main class.

For reference running Fedora 23 64bit, openjdk version "1.8.0_111".

Try using java -cp . HelloWorld

Some good reading: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/

Could Not Find or Load Main Class Error, The java <class-name> command syntax. First of all, you need to understand the correct way to launch a program using the java (or javaw )  D:\sample>java Example.class Error: Could not find or load main class Example.class Caused by: java.lang.ClassNotFoundException: Example.class Solution − The extension .class is not required while executing the program.

You need to specify classpath parameter while running your example:

java -cp . HelloWorld

What does "Could not find or load main class" mean?, . In this case it is using JRE 6 which is the reason why it worked from the command line. The error ‘Could not find or load main class’ occurs when using a java command in the command prompt to launch a Java program by specifying the class name in the terminal. The reason why this happens is mostly due to the user’s programming mistake while declaring the class.

Just a small reminder for the newbies in Java:

When compiling, you type:

javac MyClass.java

Now, you've got two files:

MyClass.class MyClass.java

Now, whereas you typed the .java extension when compiling, you must NOT type the extension .class when running the program. You should just type:

java MyClass

If you type java MyClass.class then you'll get:

Error: Could not find or load main class

Java: Could not find main class. Program will exit., By default java is searching the class file in the default directory. If your classpath is correct then above command will run the HelloWorld program. Otherwise, It will also result in Error : Could not find or load main class HelloWorld because -cp . “Could not find or load main class” If you get an error message that reads Could not find or load main class while trying to launch a Java program through a command in the command prompt then the reason behind the issue is likely a mistake on your side and most probably has to do with a wrongly declared class.

java -cp HelloWorld works. I use windows 10, and was checking out the very course. I first had to add it to path, and spent time wasted on it. Be sure, however, to NOT include the .class part of the name. Java is not my first language, but Java is portable and is a suitable language for everyone.

Netbeans - Error: Could not find or load main class, Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any). "Could not find or load main class : XXX" where the class name is XXX occurs if the CLASSPATH environment variable where java looks for all class files, does not found the class.

The same happened to me while compiling a piece of code (that was initially writen for an IDE with several files) throug terminal. The problem was mentioning the package with the same name of the main class (package HelloWorld). I fixed it and now it works. Not sure if that's your case

[Solved] Error: Could not find or load main class, 1.1 Reviews a simple Java Hello World, no package. C:\projects\HelloWorld.java. public class HelloWorld { public static  without any classpath hints, Java will look into the current directory and search for com\abc\HelloWorld.class in Windows, so if com directory exists in your current directory, your program will run otherwise you will get "Error: Could not find or load main class dto.HelloHP". 3) You can run your Java program from anywhere with the help of

How to resolve "Could not find or load main class package" in Java?, The error 'Could not find or load main class' occurs when using a java command in the command prompt to launch a Java program by  Sometimes what might be causing the issue has nothing to do with the main class. I had to find this out the hard way, it was a referenced library that I moved and it gave me the: Could not find or load main class xxx Linux. I just delete that reference and added again and it worked fine again.

Java – Could not find or load main class – Mkyong.com, tried all the solutions, but unfortunately same error showing "Could not find or load main class Duration: 8:08 Posted: Nov 20, 2019 i think you install the Code Runner Extension,so you have a Run icon in the upper right-hand corner,you could try to uninstall it,. then F1 ->Clean the java language server workspace.,run your code with F5 or the Run above your Main method

Fix: Could not find or load main class, The error could not find or load main class eclipse or load main class in Java occurs when a program runs but the main class cannot be found  C: \Users\User\Desktop > java -cp kotlin-runtime. jar _DefaultPackage Error: Could not find or load main class _DefaultPackage Which made me think that maybe I needed to add the class file to my declared classpath so:

Comments
  • can you type javac -version and java version in your cmd line and add the input to your question? Unless this solves your issue by some blatantly wrong result
  • @RyanTurnbull perhaps I should have been more specific in the post, when i run java -version the version specified is: openjdk version "1.8.0_111" OpenJDK Runtime Environment (1.8.0_111-b16) OpenJDK 64-Bit Server VM (build 25.111-b16, mixed mode)
  • That's embarrassing, something I should have been able to work out myself, so my question now is why have I been able to run java HelloWorld with out any hitches up until now on different machines?
  • It is likely your classpath was incorrect on this particular machine.You can check it by using echo $PATH , and fix it following this: introcs.cs.princeton.edu/java/15inout/classpath.html
  • This still returns "Could not find or load main class" and I have no idea why. I'm in the same directory as the class itself. Java has the worst CLI tools, I cringe every time I have to use them
  • Why do you think JDK is removed if the original post says "everything compiles fine"? Also, where is that "Select project" option that you mentioned? The question is about the command line usage of the java command.