-1

So I have only used python before and decided to learn java as my second language.I have installed both jdk(jdk-14.0.1) and jre(jre1.8.0_251) files.Now I added the PATH as C:\Program Files\Java\jre1.8.0_251\bin.Now as I run java on cmd prompt,I am getting a big documentation which I thought as the proof of successful additon of PATH.

But as I run a code on atom(yes i have set format of file as java in atom),I am still getting this error:

Selection Based runner not available for Java.

Now I also created a test file in notepad:

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

And when I run it in cmd prompt like:

javac test.java and then

java test

I am getting Error: Could not find or load main class test.

What have I done wrong

Xtense
  • 636
  • 4
  • 13
  • 3
    Does this answer your question? [What does "Could not find or load main class" mean?](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – Neeraj Bansal Jul 11 '20 at 20:15
  • @NeerajBansal I guess his question is related to atom app also, which this link: https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean don't justifies. – prashant.kr.mod Jul 11 '20 at 21:00

4 Answers4

4

The class MyClass should be in a file MyClass.java same as class name. Then you can do:
javac MyClass.java
java MyClass

Neeraj Bansal
  • 380
  • 7
  • 23
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
1

The name of the class and the file should be the same. Change test.java to MyClass.java. Additionally, you should make sure the JDK and JRE are of the same version. For instance, you have JDK 14 and JRE 8. You should either have JDK 14 and JRE 14 or JDK 8 and JRE 8.

NumberC
  • 596
  • 7
  • 17
  • I tried that but there seems to be this error:```Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.UnsupportedClassVersionError: MyClass has been compiled by a more recent version of the Java Runtime (class file version 58.0), this version of the Java Runtime only recognizes class file versions up to 52.0``` – Xtense Jul 11 '20 at 20:21
1

define it as public class MyClass.

then you will get exception as

Error: Could not find or load main class
Caused by: java.lang.ClassNotFoundException

which states the difference of names in class and the file and can be resolved by renaming any one of them.

And for your other issue, UnsupportedClassVersionError, here is the compatibility list:

The reported major numbers are:

Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

In order to generate class files compatible with Java 1.4, use the following command line:

javac -target 1.4 HelloWorld.java

Hope this helps. Chears!!!

prashant.kr.mod
  • 1,178
  • 13
  • 28
  • So I am supposed to download another version of jre? – Xtense Jul 11 '20 at 20:29
  • or compile the code in the suggested way, if you don't want to download any. – prashant.kr.mod Jul 11 '20 at 20:30
  • This helped but how I am I supposed to run my code on atom,since its showing ```Selection Based runner not available for Java.``` – Xtense Jul 11 '20 at 20:36
  • Changing file name to ```MyClass``` also works on atom.But how am I supposed to do the same without saving,since in python I don't have to save the file and just have to change the format from plain file to python.Does this not work in the case of JAVA? – Xtense Jul 11 '20 at 20:47
  • First save the file which you're trying to run in the appropriate format and then try to run it, should work. – prashant.kr.mod Jul 11 '20 at 20:56
1

Your class name should be the same as file name


To compile Java code, the file must have the extension .java. The name of the file must match the name of the class. Java does not require that the class to be public.

Eduard A
  • 370
  • 3
  • 9