8

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

Mamun
  • 375
  • 2
  • 8
  • 17

3 Answers3

9

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, or

  • Upgrade 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:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • When try to recompile using `javac -target 8 -source 8 Test.java` getting the following warning `warning: [options] bootstrap class path not set in conjunction with -source 8` *_Kindly note_I've made a mistake in my questiont. **My Ubuntu's Java version is: 8** **And javac version is: 11** – Mamun Oct 06 '18 at 00:43
  • I've try to set the Java path in my Ubuntu using the following code: `JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 PATH=$PATH:$HOME/bin:$JAVA_HOME/bin export JAVA_HOME export JRE_HOME export PATH` I'm following this, [https://stackoverflow.com/questions/9612941/how-to-set-java-environment-path-in-ubuntu] thread in Stackoverflow to set the Java path. – Mamun Oct 06 '18 at 00:51
  • I've tried different version of JDK from 8 to 11 by setting it in the path. Unfortunately no matter which version of JDK I'm adding to the path, I'm always getting javac version of 11 as followed. `javac --version javac 11` What would be the reason for such outcome? – Mamun Oct 06 '18 at 00:51
  • Consider using the [alternatives](https://linuxconfig.org/how-to-install-java-on-ubuntu-18-04-bionic-beaver-linux) utility to configure your Java default. – paulsm4 Oct 06 '18 at 05:51
  • I sorry for delay comment. I'd some urgent issues for which I could not sit with computer yesterday. I've managed to solve the issue but with different approach, though I'm not sure whether its been fully solved (because now I've problem when I'm going to install jdk 8 to my machine) The issue was that I'd different version of jdk been installed in my machine and path was not set properly. Today I've deleted jdk 8 version and add jdk 11 to the path and everything seems to work normally. I have successfully run server client app through terminal as well as other Java programs. – Mamun Oct 07 '18 at 14:25
  • I think I need a clear install of jdk to my machine after having uninstalled all the jdk versions. Unfortunately right now I can't manage this, that's why now I have to work with jdk 11 and later will do a clear jdk install. I would highly appreciate if you would kindly provide me some guidelines to for a clear jdk installation to my machine. – Mamun Oct 07 '18 at 14:28
0

I also had this problem. This is my solution. There was 2 Java versions installed (java 8 and Java SE Development kit 13). This error occurs because of this 2 versions. Just uninstall old version and you are done. (*Java compiles .java file using SE Development kit and trying to execute using old version. So compatible errors occurs.)

Then check java versions on cmd using javac -version and java -version. After uninstallation, both version codes must be same.

Lasith M
  • 48
  • 7
0

If you don't want to upgrade your Java version to Java11. Maybe you can change the version of neo4j to 3.x. neo4j 3.x compiled by Java 8, and the neo4j 4.x compiled by Java 11. Here is the download site of neo4j community edition, you can change your version of neo4j to adjust the local version of Java 8. https://neo4j.com/download-center/#community

shuisheng
  • 1
  • 1