错误:发生 JNI 错误,请检查您的安装并重试 - 在从 Ubuntu 终端运行 Java 程序期间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52671412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: A JNI error has occurred, please check your installation and try again - during running Java program from Ubuntu terminal
提问by Mamun
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.
我正在尝试通过 Ubuntu 终端运行一个用 Java 编写的简单客户端-服务器程序。不幸的是我可以成功编译代码,我无法运行代码。
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)
mamun@mamun:~$ java 测试错误:发生 JNI 错误,请检查您的安装并重试线程“main”中的异常 java.lang.UnsupportedClassVersionError:测试已由更新版本的 Java 运行时(类文件版本 55.0),此版本的 Java 运行时仅识别 java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security 中最高 52.0 的类文件版本.SecureClassLoader.defineClass(SecureClassLoader.java:142) 在 java.net.URLClassLoader.defineClass(URLClassLoader.java:467) 在 java.net.URLClassLoader.access$100(URLClassLoader.java:73) 在 java.net.URLClassLoader$1。运行(URLClassLoader.java:368) 在java.net.URLClassLoader$1.run(URLClassLoader.java:362) 在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) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) 在 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.
我试图搜索 stackoverflow 和其他论坛和博客以找到解决方案,我找到了一些类似的问题,尝试了为这些问题提供的答案,但可以为我的问题找到解决方案。这就是我在这里添加这个问题的原因。
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.
后来,我试着写了一个非常简单的Java程序,比如打印一个问候语,这个程序也可以编译但不会运行产生同样的错误。我试图从除根文件夹之外的不同文件夹中执行程序。但是所有的努力都会产生相同的结果。
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.
我可以在 Eclipse 中完美地工作,其中我的 Java 版本 8,只有在终端中工作时才会出现问题。
In my Ubuntu jdk version is 11 (which has been automatically been updated without my knowledge); My Ubuntu version:18.04.1 LTS
在我的 Ubuntu jdk 版本是 11(在我不知情的情况下已自动更新);我的 Ubuntu 版本:18.04.1 LTS
采纳答案by paulsm4
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)...
线程“main”中的异常 java.lang.UnsupportedClassVersionError:测试已由更新版本的 Java 运行时(类文件版本 55.0)编译...
You've compiled for Java 11 ... but you're running an older JRE (Java 8).
您已经针对 Java 11 进行了编译……但是您正在运行较旧的 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
与重新编译
-source
,并-target
在你的.class文件到目标的Java的早期版本,或将目标 JRE 升级到 Java 11
EXAMPLE: javac -target 8 -source 8 MyClass.java
例子: javac -target 8 -source 8 MyClass.java
FYI, these are the Java versions in each Java class file's header:
仅供参考,这些是每个 Java 类文件头中的 Java 版本:
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)
https://en.wikipedia.org/wiki/Java_class_file
- Java SE 11 = 55(0x37 十六进制)
- Java SE 10 = 54(0x36 十六进制)
- Java SE 9 = 53(0x35 十六进制)
- Java SE 8 = 52(0x34 十六进制)
- Java SE 7 = 51(0x33 十六进制)
- Java SE 6.0 = 50(0x32 十六进制)
- Java SE 5.0 = 49(0x31 十六进制)
- JDK 1.4 = 48(0x30 十六进制)
- JDK 1.3 = 47(0x2F 十六进制)
- JDK 1.2 = 46(0x2E 十六进制)
- JDK 1.1 = 45(0x2D 十六进制)
Also FYI, here are the command line options for javac:
另外仅供参考,这里是 javac 的命令行选项:
PS:
PS:
You may have multiple independent versions of Java installed at the same time. Use the alternativescommand:
您可能同时安装了多个独立版本的 Java。使用替代命令: