问题
I'm trying to run a simple client-server program written in Java through Ubuntu terminal. I could compile the code successfully unfortunately, I can't run the code.
Server class code:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
public static void main(String[] args)
{
try
{
//create server Socket with demo port
ServerSocket server = new ServerSocket(20);
//wait for server connection
Socket s = server.accept();
//upon establishing connection, print
// successful message
System.out.println("connection eastablished");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Client class code:
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
public static void main(String[] args)
{
try
{
//create client socket
Socket client = new Socket("127.0.0.1", 20);
//upon establishing connection, print
//successful message
System.out.println("connection eastablished");
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
During running the complied class, I'm getting the following error:
mamun@mamun:~$ java Test Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.UnsupportedClassVersionError: Test has been compiled by a more recent version of the Java Runtime (class file version 55.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(ClassLoader.java:763) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:368) at java.net.URLClassLoader$1.run(URLClassLoader.java:362) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:361) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
I've tried to search stackoverflow and other forum and blogs to find a solution, I've find some similar questions, tried the answers provided to those question but could find a solution for my problem. That's why I'm adding this question here.
Later, I've try to write a very simple Java program, like just to print a greeting, this program also could be compiled but would not run producing the same error. I've tried to execute the program from different folders except from the root folder. But all efforts produce the same result.
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello...");
}
}
I can perfectly work in Eclipse where my java version 8, problem occurs only when working in terminal.
In my Ubuntu jdk version is 11 (which has been automatically been updated without my knowledge); My Ubuntu version:18.04.1 LTS
回答1:
The error says it all:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Test has been compiled by a more recent version of the Java Runtime (class file version 55.0)...
You've compiled for Java 11 ... but you're running an older JRE (Java 8).
SUGGESTIONS:
recompile with
-source
and-target
to target an earlier version of Java in your .class file, orUpgrade your target JRE to Java 11
EXAMPLE: javac -target 8 -source 8 MyClass.java
FYI, these are the Java versions in each Java class file's header:
https://en.wikipedia.org/wiki/Java_class_file
- Java SE 11 = 55 (0x37 hex)
- Java SE 10 = 54 (0x36 hex)
- Java SE 9 = 53 (0x35 hex)
- Java SE 8 = 52 (0x34 hex)
- Java SE 7 = 51 (0x33 hex)
- Java SE 6.0 = 50 (0x32 hex)
- Java SE 5.0 = 49 (0x31 hex)
- JDK 1.4 = 48 (0x30 hex)
- JDK 1.3 = 47 (0x2F hex)
- JDK 1.2 = 46 (0x2E hex)
- JDK 1.1 = 45 (0x2D hex)
Also FYI, here are the command line options for javac:
Java SE 11 > Tools > javac
PS:
You may have multiple independent versions of Java installed at the same time. Use the alternatives command:
- https://linuxconfig.org/how-to-install-java-on-ubuntu-18-04-bionic-beaver-linux
来源:https://stackoverflow.com/questions/52671412/error-a-jni-error-has-occurred-please-check-your-installation-and-try-again