Home java “Could not find or load main class” when starting the program

“Could not find or load main class” when starting the program




I am trying to do the usual Hello, World , as mentioned in one of the tutorials.

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

I run it into the translator like this:

javac C: \ Java \ HelloWorld.java

Everything is going well. At least no crash messages are printed.

But when I already want to get the result, running it like this:

java HelloWorld

Then an error is displayed in the console:

Java could not find the main class HelloWorld

How to start it, what’s the problem?

Here is the complete console log:

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin & gt; javac C: \ Java \ HelloWorld.java

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin & gt; java HelloWorld
Error: Could not find or load main class HelloWorld

C: \ Program Files \ Java \ jdk1.7.0_07 \ bin & gt;

Answer 1, authority 100%

Try this:

java -classpath. HelloWorld

or like this:

java -cp. HelloWorld

Answer 2, authority 19%

I had a case when even the command

java -classpath. HelloWorld

gave an error
“Could not find or load main class”
solved this way

java -classpath "jar_name" com.list_of_your_packages.launcher

That is, you had to specify the jarnick itself in the classpath, and then specify the full path to the class with the main method

Answer 3, authority 12%

For a class in the src directory and in the basic package

package basic;
public class Main {
  public static void main (String [] args) {
     System.out.println ("Hello from Main");

works like this

$ javac src / basic / Main.java
$ java -cp src basic.Main
Hello from Main

Answer 4, authority 12%

Herbert Schildt, in his book on JDK8, provides a simple but Very useful example of compiling and running a program:


contents of Book.java file

package bookpack;
class Book {
  // ....

this file must be placed in the bookpack directory

You need to compile like this:

javac bookpack / Book.java

And execute like this:

java bookpack.Book

Do not forget that for the normal execution of the above commands, the current
there must be a directory parent of the bookpack directory.

P.S. personally checked – it works! Go for it! May the force be with you! 😉

Answer 5, authority 6%

Maybe someone will come in handy. Found another answer that helped me:

Sometimes it is useful to read not only books, but also documentation.
http://docs.oracle.com/javase/ 7 / docs / technotes / tools / windows / java.html

java command does not accept your
e: \ MyJava \ example as a path in general.

She thinks this is the name of the class. Paths are set differently. This is a big and complex topic – the classpath.

java -cp e: \ MyJava \ example

We instruct to look for classes in e: \ MyJava \ to run the class example .

Answer 6

Spent a lot of time to resolve this issue, and the solution is as follows. It is necessary to correctly register the environment variables. You need to specify the path to bin and the path to lib in the environment variables. About lib rarely speaks. If you run from the command line and the path to the libraries is not specified in the environment variables, then jvm does not know where to get them. Win 10.
For example c: \ Program Files \ Java \ jdk-10.0.1 \ lib \ and c: \ Program Files \ Java \ jdk-10.0.1 \ bin \

Good luck!

Answer 7

In environment variables, where you write the path to the JDK in the CLASSPATH variable, put “.;” before the path. I got .; C: \ Program Files \ Java \ jdk1.8.0_201 \ bin The dot means the current directory. Remember to restart Windows after editing.

Answer 8

The problem is solved by changing the startup line

instead of javac command C: \ Java \ HelloWorld.java
run like this javac C: \ Java \ HelloWorld

With the removal of the * .java file extension, the main class is discovered. The program starts up. Why this is so, I did not begin to understand.

Answer 9

  • Change to the desired directory using the console command cd ;
  • You can also add the desired path to the CLASSPATH environment variable;
  • The easiest way is to call CMD from TotalCommander by going to the desired directory.
Previous articlePause in C #
Next articleWhat is legacy code?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions