6

I create a java library program and used it in another java program as .jar file. My IDE is NetBeans. I tried the same concept via command line and I got the following error:

class file has wrong version 52.0, should be 50.0 Please remove or make sure it appears in the correct sub directory of the class path. import Demo1_Lib.Test1; ^

This are my steps.

Step 1: Created the following class library in NetBeans IDE.

  package Demo1_Lib;

 /**
  *
  * @author tveluppillai
 */
public class Test1 
{
  public void print() 
  {
    System.out.println("hello");
  }    
 }

Step 2: Create a java project on netbeans and add the jar file. (Test1.jar) and consume the class library function.

 package test2;

 import Demo1_Lib.Test1;

 /**
 *
    * @author tveluppillai
 */

  public class Test2 
  {

   /**
    * @param args the command line arguments
    */
    public static void main(String args[]) 
   {
     Test1 obj = new Test1();
     obj.print();       
    }

 } 

This compiles fine and when I ran, it gives me the right output in NetBeans

However, when I do the same thing using command prompt I got error.

I used the following command to compile and run it.

javac -cp C:\\Demo_Lib\\Test\\Test1.jar Test2.java

I got the following error:

class file has wrong version 52.0, should be 50.0 Please remove or make sure it appears in the correct sub directory of the class path. import Demo1_Lib.Test1; ^

What am I missing?

rghome
  • 8,529
  • 8
  • 43
  • 62
Teva Velu
  • 135
  • 1
  • 1
  • 13
  • After I change the right version to JDK 1.8. I got the following error: package Demo1_Lib does not exist. – Teva Velu Apr 27 '15 at 22:44
  • See [this](http://stackoverflow.com/questions/20642382/package-does-not-exists-java). How did I find it? Searching for "java package does not exist" here in stackoverflow... – ericbn Apr 27 '15 at 22:55
  • Although question is marked as duplicate - it is not exactly a duplicate as the referenced question is IDE specific whereas this is more generic. Also Accepted answer here is much better explained! >>> "try to run/reference a class compiled with JDK 8 using a runtime/compiler JRE/JDK1.6" – prash Sep 29 '17 at 08:13

2 Answers2

7

You are trying to run/reference a class compiled with JDK 8 using a runtime/compiler JRE/JDK 6.

The Java being used by the command line is probably a different version than the one used by NetBeans.

See Java class file for a list of what the numbers mean.

Download JDK8, or if you already have it, add it to your path and set JAVA_HOME.

In Unix:

export JAVA_HOME=directory
export PATH=$PATH:$JAVA_HOME/bin
rghome
  • 8,529
  • 8
  • 43
  • 62
  • I was able to compile the code. – Teva Velu Apr 27 '15 at 22:53
  • OK - but this is really a duplicate question. If you take the error message and paste it into Google you will get the answer - [class-file-has-wrong-version-52-0-should-be-50-0](http://stackoverflow.com/questions/28180915/class-file-has-wrong-version-52-0-should-be-50-0). – rghome Apr 28 '15 at 07:32
  • How do you set JAVA_HOME? And add it to the path? – Felix Doe Sep 14 '16 at 23:14
1

After I change the JDK8 to my JAVA_HOME, I was able to compile the following way and run the code...

Compile:

     javac -cp C:\\Demo_Lib\\Test\\Test1.jar Test2.java

Run:

     javac -cp C:\\Demo_Lib\\Test\\Test1.jar;**.** Test2.java
Barranka
  • 20,547
  • 13
  • 65
  • 83
Teva Velu
  • 135
  • 1
  • 1
  • 13