5

I have created a windows application using java 8 . But my client is using java 7 on his machine and not able to upgrade java on the machine.

So when i run jar on java7 compiled in java 8 it gives error so can i convert jar compatible to lower version java

adesh singh
  • 1,727
  • 9
  • 38
  • 70

3 Answers3

6

You can't change a compiled jar's Java version. You have 2 option in hand.

  1. Compile the Source code using Java-7.

  2. Compile source code using Java-8 but using the following command when target vm version is java-7.

javac "Your java classes" -source 1.8 -target 1.7
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • 1
    It's actually quite doable to change the class file format version after the fact (unless you have used new Java 8 API or a language feature like lambdas). – Sartorius Apr 12 '19 at 13:52
  • What if one does not use any jdk8 features, compile 1.8 and target 1.8. Will it work? – Gaurav Nov 19 '19 at 10:45
5

Java 7 is forward compatible with Java 8. Java 8 can be compiled so that it runs on a Java 7 VM (with -source 7 -target 7), but you can’t use any of the newer APIs. Just Compile with JAVA 7 and you would be good to go just make sure you are not using new features of java 8 like lamda

Vaibhav Gupta
  • 638
  • 4
  • 12
  • What if one does not use any jdk8 features, compile 1.8 and target 1.8. Will it work? – Gaurav Nov 19 '19 at 10:46
  • @Gaurav sure it will work, I dont see problem.. do you mean you are writting code like you are writting java 7 but compailing to java 8? I think it will work..! not 100% sure but think it will work. – acakojic Jan 27 '21 at 11:56
0

You can't. You need to compile with Java 7. You can run multiple Java version and choose which version should be used to compile: Multiple Java versions running concurrently under Windows

user1209216
  • 7,404
  • 12
  • 60
  • 123
  • 2
    No, you can build a Java 7 binary with javac of Java 8. See target flag of javac: https://stackoverflow.com/questions/32851611/javac-source-and-target-options-usage – spi Apr 12 '19 at 09:34
  • It looks you are right, I did not know that – user1209216 Apr 12 '19 at 09:49