"Could not find or load main class" when running the program
I am trying to perform the usual Hello, World, as it is said in one of the manuals.
class HelloWorld
{
public static void main(String args [])
{
System.out.println("Hello World");
}
}
I run it in the translator like this:
javac C:\Java\HelloWorld.java
Everything goes well. At least, no crash messages are being output.
But when I already want to get the result, running like this:
java HelloWorld
Then the console displays an error:
Java could not find the main class HelloWorld
How to run it, what is it? a problem?
Here is the full log of what is happening in the console:
C:\Program Files\Java\jdk1.7.0_07\bin>javac C:\Java\HelloWorld.java
C:\Program Files\Java\jdk1.7.0_07\bin>java HelloWorld Error: Could not find or load main class HelloWorld
C:\Program Files\Java\jdk1.7.0_07\bin>
9 answers
Try this way:
java -classpath . HelloWorld
Or so:
java -cp . HelloWorld
Otherwise, java cannot find the compiled class.
I had a case where even the command
java -classpath . HelloWorld
Gave the 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 jar itself in classpath, and then specify the full path to the class with the main method
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");
}
}
It works like this for me
$ javac src/basic/Main.java
$ java -cp src basic.Main
Hello from Main
Maybe someone will need it. I 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
The Java command does not perceive your
e:\MyJava\example
as a path in general.
She thinks it's a class name. The paths are set differently. This is a large and complex topic-classpath.
java -cp e:\MyJava\ example
We specify to search for classes in e:\MyJava\
to run the class example
.
Herbert Schildt in his little book on JDK8 gives a simple but A very useful example of compiling and running a program:
EXAMPLE:
The contents of the Book file.java
package bookpack;
class Book {
//....
}
This file must be placed in the bookpack
Compile it like this:
javac bookpack/Book.java
And run like this:
java bookpack.Book
* * * ATTENTION ***
Do not forget that for the normal execution of the above commands, the current
directory must be a directory that is parent to the bookpack directory.
P.S. I personally checked it-it works! Go ahead! May the force come with you! ;)
I spent a lot of time to solve this issue, and the solution is as follows. You need to correctly register the environment variables. You need to specify the path to bin
and the path to lib
in the environment variables. Very few people talk about lib
. If you run from the command line and the path to the libraries is not specified in the environment variables, the 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!
In the environment variables where you write the path to the JDK to the CLASSPATH variable, put".; "before the path. I got .;C:\Program Files\Java\jdk1.8.0_201\bin
The dot indicates the current directory. After editing, don't forget to restart Windows.
The problem is solved if you change the start line
Instead of the javac command C:\Java\HelloWorld.java run like this javac C:\Java\HelloWorld
With the removal of the file extension *.the java main class is detected. The program starts. Why exactly, I did not understand.
- Navigate to the desired folder using the console command cd;
- You can also add the desired path to the environment variable CLASSPATH;
- The easiest way is to call CMD from TotalCommander, going to the desired cathalo.