错误:发生了JNI错误,请从Ubuntu终端运行Java程序期间检查安装并重试 [英] Error: A JNI error has occurred, please check your installation and try again - during running Java program from Ubuntu terminal

查看:153
本文介绍了错误:发生了JNI错误,请从Ubuntu终端运行Java程序期间检查安装并重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Ubuntu终端运行一个用Java编写的简单客户端服务器程序.不幸的是,我可以成功编译代码,但是我无法运行代码.

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.

服务器类代码:

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();
                }
        }
}

客户端类代码:

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测试错误:发生了JNI错误,请检查您的安装,然后重试线程"main"中的异常java.lang.UnsupportedClassVersionError:测试已由Java Runtime的最新版本(类文件版本55.0),此版本的Java Runtime仅识别类文件版本在java.lang.ClassLoader.defineClass1(本机方法)处最大为52.0java.lang.ClassLoader.defineClass(ClassLoader.java:763)在java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)在java.net.URLClassLoader.defineClass(URLClassLoader.java:467)在java.net.URLClassLoader.access $ 100(URLClassLoader.java:73)在java.net.URLClassLoader $ 1.run(URLClassLoader.java:368)在java.net.URLClassLoader $ 1.run(URLClassLoader.java:362)在java.security.AccessController.doPrivileged(本机方法),位于java.net.URLClassLoader.findClass(URLClassLoader.java:361)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)在java.lang.ClassLoader.loadClass(ClassLoader.java:357)在sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

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)

我尝试搜索stackoverflow以及其他论坛和博客以找到解决方案,我找到了一些类似的问题,尝试了提供给这些问题的答案,但可以为我的问题找到解决方案.这就是为什么我在这里添加这个问题.

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.

稍后,我尝试编写一个非常简单的Java程序,就像打印问候一样,该程序也可以编译,但不会运行时会产生相同的错误.我尝试从根文件夹以外的其他文件夹执行程序.但是所有的努力都会产生相同的结果.

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...");
                }
}

我可以在Java版本8的Eclipse中完美工作,仅当在终端中工作时才会出现问题.

I can perfectly work in Eclipse where my java version 8, problem occurs only when working in terminal.

在我的Ubuntu jdk版本中是11(在我不知情的情况下已自动更新);我的Ubuntu版本:18.04.1 LTS

In my Ubuntu jdk version is 11 (which has been automatically been updated without my knowledge); My Ubuntu version:18.04.1 LTS

推荐答案

错误说明了一切:

线程主"中的异常java.lang.UnsupportedClassVersionError:测试已由Java Runtime的较新版本进行编译(类文件版本55.0)...

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

您已经针对Java 11进行了编译……但您正在运行的是较旧的JRE(Java 8).

You've compiled for Java 11 ... but you're running an older JRE (Java 8).

建议:

  • -source -target 重新编译,以将.cl​​ass文件中的Java早期版本作为目标,或者

  • recompile with -source and -target to target an earlier version of Java in your .class file, or

将目标JRE升级到Java 11

Upgrade your target JRE to Java 11

示例: javac -target 8 -source 8 MyClass.java

仅供参考,这些是每个Java类文件标题中的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)
  • Java SE 10 = 54(16x 0x36)
  • Java SE 9 = 53(十六进制0x35)
  • Java SE 8 = 52(十六进制0x34)
  • Java SE 7 = 51(16x 0x33)
  • Java SE 6.0 = 50(16x 0x32)
  • Java SE 5.0 = 49(16x 0x31)
  • JDK 1.4 = 48(0x30十六进制)
  • JDK 1.3 = 47(十六进制0x2F)
  • JDK 1.2 = 46(十六进制0x2E)
  • JDK 1.1 = 45(十六进制0x2D)

也供参考,这是javac的命令行选项:

Also FYI, here are the command line options for javac:

Java SE 11>工具> javac

PS:

您可能同时安装了多个独立的Java版本.使用替代方法命令:

You may have multiple independent versions of Java installed at the same time. Use the alternatives command:

这篇关于错误:发生了JNI错误,请从Ubuntu终端运行Java程序期间检查安装并重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆