How to Compile a .Java with Support for Older Versions of Java

How do I compile a .java with support for older versions of Java?

Yes, you can set the version of compiler at compile time. And compile your java code into old versions of java.

From Oracle article :
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html

Cross-Compilation Example

Here we use javac to compile code that will run on a 1.4 VM.

% javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \
-extdirs "" OldCode.java

You might also need following parameter to set denote the version of your code.

-source release

-Specifies the version of source code accepted.

Compile Java Program in Eclipse with Older JRE Without Installing

  • In the Project Explorer view, right-click on the project and then select Properties.
  • Select the Java Compiler page in the in the Properties window.
  • In the JDK Compliance section, select the desired Compiler compliance level.
  • Click Apply and then Ok.

It will automatically compile to the right version

Note this doesn't actually change the version, just that the compiler would be able to enforce the rules that another Java version would. So it should be compatible

For cross verfiying if it is compiled properly with the lower version,
1. Go to properties
2. go to java build path
3. go to libraries,
4. click on JRE System Library-- double click this to see if exceution environment is properly set.

How to Run Jar on java 7 compiled in java 8 or higher version

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

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

The version number shown describes the version of the JRE the class file is compatible with.

The reported major numbers are:

Java SE 19 = 63,
Java SE 18 = 62,
Java SE 17 = 61,
Java SE 16 = 60,
Java SE 15 = 59,
Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
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

(Source: Wikipedia)

To fix the actual problem you should try to either run the Java code with a newer version of Java JRE or specify the target parameter to the Java compiler to instruct the compiler to create code compatible with earlier Java versions.

For example, in order to generate class files compatible with Java 1.4, use the following command line:

javac -target 1.4 HelloWorld.java

With newer versions of the Java compiler you are likely to get a warning about the bootstrap class path not being set. More information about this error is available in a blog post New javac warning for setting an older source without bootclasspath.

Does the JDK version being used to compile the code matter?

No, you can't necessarily run code compiled with a new JDK on an old JRE. Compiled classes contain a version number for the class file format; if this is newer than the runtime expects, it will refuse to load the class.

Most Java compilers support an option to target an older JRE, generating an older class file format than the compiler was built for. However, you can still run into trouble if you don't also compile against an older version of the Java runtime library. Your code might use new API that isn't in the older version of Java. The compiler, with its current version of the API, won't catch this even when you specify an older target.

For the standard javac compiler in OpenJDK, these options are -target and -bootclasspath. You might also want to set the -source option to catch usage of newer language features that require support the older class files don't provide.

how to check the jdk version used to compile a .class file

You're looking for this on the command line (for a class called MyClass):

On Unix/Linux:

javap -verbose MyClass | grep "major"

On Windows:

javap -verbose MyClass | findstr "major"

You want the major version from the results. Here are some example values:

  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55

Java code that compiles in older Oracle versions gives compile error in Oracle 12.2

Turns out that server-side SQLJ is no longer supported in Oracle 12.2. A little rewrite of the code did the trick.

create or replace and compile java source named "DirList" as

import java.io.*;
import java.sql.*;

public class ChpDirList {

public static void getList(String directory)
throws SQLException {

Connection conn = DriverManager.getConnection("jdbc:default:connection:");
String sql = "INSERT INTO NGM_DIR_LIST (file_name, file_length, file_type) values (?,?,?)";

File path = new File(directory);
String[] list = path.list();
String element;

for (int i = 0; i < list.length; i++) {

element = list[i];

String fpath = directory + "/" + list[i];

File f = new File(fpath);

long len;

String ftype;

if (f.isFile()) {

len = f.length();
ftype = "F";

} else {

len = 0;
ftype = "D";
}

PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, element);
pstmt.setLong(2, len);
pstmt.setString(3, ftype);

pstmt.executeUpdate();
pstmt.close();
}
}
}

Can newer JRE versions run Java programs compiled with older JDK versions?

You would not encounter any problems - that's the magic of Java -it's backwards compatible.You can run almost all code from Java 1 on Java 8. There's no reason why Java 6 code won't run on a Java 8 Runtime.

What is interesting, is that for applications written in, let's say, Java 1.4, you even have speed increases when running them on later runtimes. This is because Java is constantly evolving, not just the language known as "Java", but also the JVM (Java virtual machine). I still have source code from more than 10 years ago that still work, as expected in the latest JVM.

If you want to target, let's say, a Java 5 VM, then you can do that with the Java 8 SDK tools. You can ultimately specify which target VM you wish to support, as long as you bear in mind that a version 5 VM might not support all the features a version 8 VM will.

I've just tested code I wrote in Java 5 against the new Java 8 runtime and everything works as expected, so, even though we have a more powerful language and runtime now, we can continue to use our investments of the past. Just that alone makes Java a great development choice for companies.



Related Topics



Leave a reply



Submit