-2

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

nyarian
  • 4,085
  • 1
  • 19
  • 51
  • Please reformat your code so it's easier to read. – UmarZaii Aug 13 '17 at 08:56
  • I tried this command before, when the command line's "location" was in the source folder of the project (src): "java cards.Card". It did not work. But it did when I added the -cp as the command parameter manually. So, Tuyen Nguyen's solve had really helped. That's something wrong with my CLASSPATH environment variable, and it is obviously possible to happen to other people. Similiar question here: https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean?rq=1 ,and the best answer (plus two top voted) is describing another solve for this. – nyarian Aug 13 '17 at 15:56
  • about "simple typographical error": http://screenshot.su/show.php?img=1cf41c141fdc9bd60056965b7ba6793f.jpg – nyarian Aug 13 '17 at 19:35

2 Answers2

0

Open the CMD in the location where you have the Card.class

 set CLASSPATH=""
 java Card

I assume you don't have package deceleration on Card.java

Imran
  • 1,732
  • 3
  • 21
  • 46
  • naa, the same error shows up. – nyarian Aug 12 '17 at 18:34
  • Also the code you have posted does not contain package declaration, the problem is very similar. If you have package deceleration lets say "package cards" in your class, you can compile it like javac Card.java without an issue. But if you try to run it, it will throw the exception "main not found", you need to go up one level and execute "java card.Card" . Not idea what else might happen. – Imran Aug 12 '17 at 19:10
  • i tired these variations too, as i described in the question, but it didn't help without setting classpath to the project's source folder – nyarian Aug 13 '17 at 07:28
  • The original code you posted did not have "package cards;". And as I mentioned the problem is "Also the code you have posted does not contain package declaration, the problem is very similar. If you have package decleration lets say "package cards" in your class, you can compile it like javac Card.java without an issue. But if you try to run it, it will throw the exception "main not found", you need to go up one level and execute "java card.Card" ." – Imran Aug 13 '17 at 16:01
  • i didn't understand what u had ment by "package deceleration". after u said "declaration", i updated my question. by the way: http://screenshot.su/show.php?img=1cf41c141fdc9bd60056965b7ba6793f.jpg – nyarian Aug 13 '17 at 19:34
0

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.

TuyenNTA
  • 1,194
  • 1
  • 11
  • 19