0

I'm not sure if this question belongs here, so if it doesn't, let me know.

When I try to use the command java classname it gives me the error Cannot find or load main class, but when i compile it just in Eclipse i don't get that error, the problem is with my cmd.

When i go inside the source directory where my project is which is C:\Users\hp\eclipse-workspace\ServerMT\bin, and type "dir" they list that my class is in there, but when i run the command it just doesn't work.

Now I somehow have two versions of java which is jdk-13.0.2 and jdk-15, both inside the programs java folder, i really don't know which one of them to use or each one of them works or if that's the source of the problem.

I kept updating the environment variables switching between ones for jdk-15 and one for jdk-13, nothing works, I really don't know how to solve this, been trying to for a week, any help will be appreciated.

And when i run the command echo %CLASSPATH% the result is C:\Program Files\Java\jdk-15\lib*.jar, but when I type this in search on my pc I can't find the folder, I only find C:\Program Files\Java\jdk-15.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
joxavy
  • 9
  • 8

3 Answers3

0

You don't need any environment variables except PATH, which should point to the java version you want.

And especially, don't use CLASSPATH. Thus you prevent the JVM from finding your class in the current directory.

Ingo
  • 36,037
  • 5
  • 53
  • 100
0

Well many problems can cause this, first thing:

  • If your class is inside a package (and it is highly recommended to be this way) then you must write something like java mypackage.MyClass, and you must be in root of your classes folders (where mypackage folder exists).

  • Have you compiled your source files? What do you see there MyClass.class or MyClass.java?

I think you are a newbie and trying to learn java, if that's the case you probably don't use any library, so CLASSPATH variable is not probably your problem, but it is better to remove CLASSPATH from your environment variables.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
  • yes i'm new to this,my class isn't inside any package it's just the default package in eclipse, i complied the code and it works on eclipse, when i run it in cmd and check"dir' i see "Myclass,class" but when i try to type java myclass it doesnt work, i deleted the classpath and it stil doesn't work – joxavy Nov 17 '20 at 10:02
0

Navigate to directory C:\Users\hp\eclipse-workspace\ServerMT\bin and enter the following command:

java -cp . ServerMT

CMD screen capture

Refer to the documentation for the java command

This question and answer may also be helpful.
How to know the command line used by Eclipse to run the java program?

In the package explorer view of Eclipse, the project JDK should appear.

Eclipse package explorer view

Abra
  • 19,142
  • 7
  • 29
  • 41