-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

2

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

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!!!

5 Comments

So I am supposed to download another version of jre?
or compile the code in the suggested way, if you don't want to download any.
This helped but how I am I supposed to run my code on atom,since its showing Selection Based runner not available for Java.
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?
First save the file which you're trying to run in the appropriate format and then try to run it, should work.
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.

1 Comment

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
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.