Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile class files with the Java 7 SDK which can run on Java 6 JVMs?

Tags:

Since the public Java 6 SE JRE is getting closer to it's EOL (Nov '12), I'm considering porting my projects from Java 6 to Java 7. This would'nt be a big deal, if Apple would provide a Java 7 JRE for Mac OS X. But since Apple isn't willing to do so, I still have to support users which only have a Java 6 JRE.

Is there a way to compile Java 6 compatible binaries (class files) with the Java 7 javac? Certainly I'm aware that I can't use the new Java 7 features when doing so.

Thanks in anticiption!

like image 946
t3chris Avatar asked Apr 11 '12 14:04

t3chris


People also ask

Can program developed with Java 7 be run on Java 8?

Binary CompatibilityJava SE 8 is binary-compatible with Java SE 7 except for the incompatibilities listed below. Except for the noted incompatibilities, class files built with the Java SE 7 compiler will run correctly in Java SE 8.

Is Java 1.7 the same as Java 7?

all the way to 1.7, also known as Java 7) usually contain improvements to both the JVM and the standard library, so the two usually need to run together, and are packaged together in the JRE. If you are running any Java program on your computer, you have a JRE installed. The JDK is the Java Development Kit.

Which command will be used to compile the Java class?

The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

What is a javac executable?

With Java S W, you write your human-readable source code in .java files. Before executing this code, you need to compile this code to bytecode, which is the machine-readable version of your code. This compiled version of your code takes the form of .class files. The "javac" executable is the Java compiler.

How to execute Java class files from the command line?

This tutorial demonstrates how to execute Java class files from the command line. Java class files are compiled Java files that the Java Virtual Machine executes. Let’s try to run the following Java class through the command prompt. package delftstack; public class Demo { public static void main(String[] args) { System.out.println("Hello!

Where is the source code of a Java program stored?

The source code is placed in the file com/baeldung/javac/Data.java. Note that we use *nix file separators in this article; on Windows machines, we must use the backslash (‘ \') instead of the forward slash (‘ /' ). 4. Standard Options

What are the different types of javac file types?

Notice that source files should be arranged in a directory hierarchy corresponding to the fully qualified names of the types they contain. Options of javac are categorized into three groups: standard, cross-compilation, and extra. In this article, we'll focus on the standard and extra options.


2 Answers

It depends. If your program doesn't use the new Java 7 language extensions, then you can run the Java compiler with the -source 1.6 and -target 1.6 options. But if you use Java 7 language extensions then -source 1.6 will result in compilation errors.

Certainly I'm aware that I can't use the new Java 7 features when doing so.

That includes Java 7 language features ... and dependencies on Java 7 changes to the standard class library APIs. Also be aware that there are small number of behavioural differences (aka API bug fixes) that may cause code to run differently on Java 6 and Java 7. They should be described in the Java 6 to Java 7 transition document.


UPDATE - This probably no longer an issue for you anyway, because Oracle have released Java 7 for Mac OSX.

like image 71
Stephen C Avatar answered Sep 18 '22 14:09

Stephen C


i have jdk6 installed. if you check the man page of javac:

Cross-Compilation Options           By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compil‐           ing, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspath           and -extdirs when cross-compiling; see Cross-Compilation Example below.           -target version             Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier             versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6). 
like image 42
Kent Avatar answered Sep 21 '22 14:09

Kent