-1

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.

  • 4
    Is the class name `frametester` or `FrameTester`? – Slaw Apr 11 '19 at 21:16
  • What Slaw said, plus your cut and paste seems to be missing stuff. – Joseph Larson Apr 11 '19 at 21:17
  • 2
    Afaik `C:\Program Files` is a protected directory by default on Win10, i.e. you need administrator access to write there. You should try another directory, e.g. your 'Documents' directory. – Jorn Vernee Apr 11 '19 at 21:19
  • I just realized that I copy pasted an older version onto the question. It's `FrameTester` on the current one that doesn't work. – birdWithTeeths Apr 11 '19 at 21:19
  • 1
    Avoid using the /Program Files or /Program Files (x86) or /Windows directories for development workspace. By the same token, if you're developing Java EE Apps, don't put containers (such as glassfish) in those directories either. You need a readable/writeable directory for development. – Christian Hur Apr 11 '19 at 21:24
  • I just tried putting it in the documents folder, and now it doesn't say anything when I try to run it. command prompt just waits a few seconds and then goes to the next line. what's happening now? – birdWithTeeths Apr 11 '19 at 21:26
  • `javac` is just the compiler, you need `java FrameTester` to execute (after successfully executing `javac`) – user85421 Apr 11 '19 at 22:14
  • thanks! that gave me a more detailed error message. The problem seems to be with the installation. I will add the error to the question. – birdWithTeeths Apr 12 '19 at 02:27
  • Possible duplicate of [java -version and javac -version showing different versions](https://stackoverflow.com/questions/25074017/java-version-and-javac-version-showing-different-versions) – Slaw Apr 12 '19 at 19:01
  • hmm. maybe. I'll look at it. – birdWithTeeths Apr 12 '19 at 21:59

2 Answers2

0

Your FrameTester.java program is correct, without any issues.

It looks like the PATH and JAVA_HOME variable are messed up in your environment. I would suggest to print both of them on command prompt using echo %PATH% and %JAVA_HOME% command and confirm the JDK version used is correct. i.e. both PATH and JAVA_HOME are pointing to same JDK or JRE folder. If not fix the issue. Hopefully it will resolve your problem.

Mayank Maria
  • 146
  • 5
  • Unfortunately, I can't get to %JAVA_HOME% for some reason (It's not causing any other issues, so I'm not going to even bother.), so I wasn't able to use that part, but looking at the PATH variable helped me identify what was going wrong, so that helped a lot. Thank you! – birdWithTeeths Apr 16 '19 at 04:32
0

I FIXED IT! Long story short, I screwed up the installation, and then proceeded to screw it up even more by uninstalling and reinstalling jre and jdk almost at random in the hopes that it might fix my issue. All of this might have worked, had I not have done half of it before the update to java 12. I had jre from Java 8 and jdk from java 12. I also had two different versions of the java 8 jre somehow. I think I accidentally installed a 32 bit version of jre because I didn't understand the install page.

I just deleted the extra jre (that was in Program Files (x86) instead of in Program Files where the rest of everything was.) and deleted jdk 12 and installed the jdk from java 8, deleted some extra weird path variables, added a new one, and that fixed it once I restarted my computer