Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javac Cross-Compilation with 1.7

So guys,

I'm trying to play a bit with Javac Cross compilation with Ant and on terminal. Locally and on an integration environment and i'm having the same problem on the very basic problem.

I run this in the linux terminal (and also on my cygwin on windows and the cmd):

 javac -target 1.6 -source 1.7 -bootclasspath /usr/java/jdk1.6.0_27/jre/lib/rt.jar Main.java

with Main.java with nothing other than a System.out.println.

javac -version ==> javac 1.7.0_11

I'm getting the error message:

javac: source release 1.7 requires target release 1.7

I have roughly the same configuration on my local windows machine with the exact same results.

It was my understanding that cross compilation is all about compiling some source code that is compatible with a higher version jdk using that higher version of jdk, but passing the rt.jar of the target version that is supposedly lower.

if target and source are the same, it worked.

target=1.7 and source=1.7 workd fine

target=1.6 and source=1.6 worked just fine

but i want cross-compilation, so what is it that i'm doing wrong?

I appreciate all the help I could get and thanks in advance.

like image 541
Eyad Ebrahim Avatar asked Aug 19 '13 18:08

Eyad Ebrahim


People also ask

Does javac support cross-compiling?

But javacalso supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspathand -extdirswhen cross-compiling; see Cross-Compilation Examplebelow.

How do I use the javac command to compile Java code?

The javac command supports the new Java Compiler API defined by the classes and interfaces in the javax.tools package. To compile as though providing command-line arguments, use the following syntax: The example writes diagnostics to the standard output stream and returns the exit code that javac would give when called from the command line.

How to use javac-help to view compiler options?

Syntax of this command is: Type javac -help to view compiler options, and type javac -version to know current version of the compiler. By default, the generated .class files are placed under the same directory as the source files. 1. Compile a single Java source file 2. Compile multiple Java source files Compile all source files: 3.

How do I find the compiler version in javac?

Syntax of this command is: javac [options] [source files] Type javac -help to view compiler options, and type javac -version to know current version of the compiler. By default, the generated.class files are placed under the same directory as the source files.


2 Answers

You cannot have a newer version of source and lower version of target. For example, In Java 5, a number of new features were added to the language, such as generics, autoboxing and you cannot expect a JVM 1.4 to understand it. So, you must tell the compiler that your source code is Java 1.4 source code. This explains the results you have.

The default for -target depends on the value of -source:

  • If -source is not specified, the value of -target is 1.7
  • If -source is 1.2, the value of -target is 1.4
  • If -source is 1.3, the value of -target is 1.4
  • If -source is 1.5, the value of -target is 1.7
  • If -source is 1.6, the value of -target is 1.7
  • For all other values of -source, the value of -target is the value of -source.

For more info refer to http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

like image 69
vinay Avatar answered Sep 19 '22 04:09

vinay


This is a limit in javac. Note that you could get away with just specifying "-target" (and not -source) in older versions of javac. You might still be able to.

You may want to consider using the Eclipse Java Compiler (ecj) which is available as a standalone compiler, as a maven plugin and which also can be used by the javac task in ant scripts.

See http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.user/tasks/task-using_batch_compiler.htm for details.

like image 41
Thorbjørn Ravn Andersen Avatar answered Sep 21 '22 04:09

Thorbjørn Ravn Andersen