Target different version of JRE
Asked Answered
C

3

13

I'm testing Java on a Windows Surface Pro. The Surface Pro has Java 7 Update 55, but does not have the JDK installed.

I compiled a program on my MacBook from the command line using javac. The MacBook Has Java 8 Update 5 and it includes the JDK (obviously because I compiled on the MBP).

When I move the program from the MackBook to the Surface Pro, I get the following error. Here, "moving the program" means copying the two *.class files to the new machine and trying to execute the byte codes.

java.lang.UnsupportedClassVersionError: HttpsTest : Unsupported major.minor version 52.

Trying to compile with target information results in:

$ javac -target 1.7 HttpsTest.java SSLSocketFactoryEx.java
javac: target release 1.7 conflicts with default source release 1.8

javac -help does not list any options to control the version information of the compiled program (other than target, which does not appear to work).

How do I compile my source files for down level versions of a JRE? In this case, I'm targeting Java 7 from a Java 8 machine.

Conyers answered 13/5, 2014 at 9:34 Comment(0)
T
10

From the command line, change your call to:

javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java

The documentation for javac (for JDK 8) is here.

The documentation for javac (for JDK 9) is here.

Note: In JDK 9, -target is replaced with --release.

Tentative answered 27/10, 2017 at 18:36 Comment(2)
Thanks for your answer.Rankle
javac --release 7 Tmp.java for javac with version 9 ~ 11, the available numbers for --release are 7, 8, 9, 10, 11.Caveator
G
2

If you are using eclipse you can set compiler compliance level in the project properties-> Java Compiler. So your code is compiled for the chosen Java version. See here:

enter image description here

Gapeworm answered 27/7, 2017 at 11:36 Comment(1)
@Rankle I answered your question in a separate post.Tentative
B
0

In the error message major.minor version 52. the number 52 here represents the java version with which the class is compiled for example

Java SE 9 = 53
Java SE 8 = 52
Java SE 7 = 51
Java SE 6.0 = 50
Java SE 5.0 = 49
JDK 1.4 = 48
JDK 1.3 = 47
JDK 1.2 = 46
JDK 1.1 = 45

so your installed JRE is at higher version than your target use -source with the version number to which you want to compile in your case:

javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java

Bosket answered 17/4, 2018 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.