I made a hello world program in windows notepad, but I keep getting a class error when I try to run it.
I copy pasted the code from an excel document originally, so I tried completely rewriting it in case it was some sort of font problem, but no luck. I also thought it might be some sort of problem with the way I installed Java, but I have uninstalled everything and reinstalled it again multiple times, so unless I keep doing it wrong, I don't know if that's the problem.
here's the code:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextField;
import java.awt.Color;
public class FrameTester{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(200,200);
JTextField text = new JTextField("Hello, World!");
text.setBackground(Color.BLUE);
frame.add(text);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
the java folder in program files has two folder in it: one called jdk-12 and the other called jre1.8.0_202. they were installed on different days. the jdk folder was installed first. I have an environment variable called C:\Program Files\Java\jdk-12\bin
It was supposed to make a window pop up saying "Hello world" on a blue background. The same program worked for someone else on a different computer. We were both using windows 10. I have never been able to get a java program to run on my computer. I tried a different hello World program before, and I got the same kind of error.
this was what I put in the command prompt to launch it:
C:\Program Files>javac FrameTester.java
this is what the error said:
FrameTester.java:5: error: error while writing FrameTester: C:\Program Files\FrameTester.class
public class FrameTester{
^
1 error
I have no idea what the problem is. help!
UPDATE: I put the program in the documents folder and tried to run it, and now it's giving me a more detailed error message, but I still can't figure out how to fix it.
C:\Users\orang\Documents>java FrameTester
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: FrameTester has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
when I do java -version
, I get this:
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
but when I do javac -version
, I get this:
javac 12
I'm pretty sure they're supposed to be the same. I must have messed up somewhere in the installation. I really don't know what to do at this point.
EDIT: Thanks for flagging the question as duplicate. the answers on the other one really helped. For some reason echo %JAVA_HOME%
doesn't return anything, so I couldn't use that to fix my problem. I had to do something else to solve it. I think I'm going to leave this up just in case someone is like me and doesn't know what they're doing when they install java and accidentally screws it up really, really, bad. Thanks for the help.