Search code examples
javaconsoleenvironment-variablesclasspathjavac

java console error could not find or load main class


I have a class with the main method. When i'm using the Windows command line to run the main method of the class, the compiling process with "javac" command works well (current location for the command line - directory with this class). File named "Card.class" gets created nearly to "Card.java" file. But command "java Card" returns error with the description "Could not find or load main class". In Intellij Idea this method within the class works completely well.

What i've tried to do to solve the problem (some solves were taken from StackOverflow topics):

1)Full reinstall of JDK and JRE.

2)Rewrote all my environment variables:

JAVA_HOME = C:\Program Files\Java\jdk1.8.0_144

JDK_HOME = %JAVA_HOME%

PATH = C:\Program Files\Java\jdk1.8.0_144\bin

CLASSPATH = %JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\jre\lib\tools.jar

3)I tried different variations for the "CLASSPATH" environment variable. Also, I manually wrote this: "java -classpath "$s" Card", where the value for "-classpath" parameter i copied from Intellij Idea command, that had been returned after running the program in the IDE.

4)Tried some variations for class name in the command (from just "java Card" to "java package_name.Card" in the source project directory). All of these variations were recognized by the command line, returning the same error.

I'm thinking that the problem with the "classpath" variable, but all solutions i found are suggesting different values for this variable, and any of them had worked. Last updated value of the variable is mentioned before.

Here is my class (possible that the problem is here):

package cards;

public class Card {

    //Constants, constructor, getters, setters, validation checking methods

    public static void main(String[] args) {

        // Just to see in the command line that the method was invoked
        System.out.println("WORK");

        // "assert" checks for constants values

    }
}

Stacktrace for my error:

myPathToTheProject\src\cards>java Card

Error: Could not find or load main class Card


Solution

  • Let's set classpath correct before run. This work fine for me: java -cp ./ Card or java -cp . Card. The part: -cp . or -cp ./ is for setting classpath to current folder.