What JDK distributions can run `javac -source 1.6 -target 1.5`?

NOTE. Please do not comment on all the risks of cross-compiling. Thank.




I have a situation where we need to build the Java 6 source code for the Java 5 JVM (to make sure that using JAX-WS is correct). We used to do this with an ant ant script (which apparently can), but after switching to Maven, we found that it ends with a javac complaint:

$ javac -source 1.6 -target 1.5 javac: source release 1.6 requires target release 1.6 

Is there any Java distribution for Linux (Ubuntu 11.10, x86) where javac can do this?




EDIT: This is not the case since the restriction in javac is the same. The solution (which made this problem go away) was to switch from the default javac compiler to the eclipse compiler in the maven-compiler-plugin.




EDIT: I found that the Eclipse compiler generates bytecode for anonymous inner classes that the javadoc utility disagrees with. I am preparing an error report for this problem.

+3
java linux ubuntu javac
Feb 13 '12 at 12:23
source share
2 answers

According to the documentation ( Java 5 , Java 6 ), the Oracle SDK should be able to do this by following the instructions in the cross-compilation example .

Java 6 should support any version from 1.3 to 1.6 as -target ; it says nothing about what happens when you use generics and other "compatible" functions in the source. The compiler should be able to trim them.

Another culprit in the game may be javac : the compiler can handle this set of arguments, but the command line tool may be offended.

In this case, write your own command line using the Java Compiler API . This may allow you to get some tricks that you cannot achieve otherwise.

You can also try the Eclipse compiler (see " Using the Batch Compiler ).

This may fail due to the way Java works: Java X code can run in Java Y as long as X <= Y. So, although you can easily compile Java 1.4 code for the Java 6 virtual machine, the opposite not always true.

If all else fails, write a preprocessor that reads the source code and breaks up unsupported elements (for example, @Override on interfaces). As long as you compile code with annotations once in a while using Java 6, the converted code should also be safe (if your code strip has no error ...)

+6
Feb 13 '12 at 13:23
source share

This answer is an implementation of what @ ThorbjørnRavnAndersen explained in the comments as a solution. Using the sample code from here and fixing a few typos, I was able to come up with an example using the Eclipse compiler.

Calculator.java

 package example; // there needs to be a package to avoid A "@WebService.targetNamespace must be specified on classes with no package" // when running this import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint; @WebService public class Calculator { @WebMethod public int add(int a, int b) { return a+b; } public static void main(String[] args){ // create and publish an endpoint Calculator calculator = new Calculator(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator); } } 

pom.xml

 <project> <modelVersion>4.0.0</modelVersion> <groupId>fi.eis.applications</groupId> <artifactId>ws-calculator</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.6</source> <target>1.5</target> <compilerId>eclipse</compilerId> </configuration> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-compiler-eclipse</artifactId> <version>2.6</version> </dependency> </dependencies> </plugin> </plugins> </build> </project> 

What you can compile with mvn clean compile and then run with java Calculator in the target/classes/example folder. It will launch a web service on port 8080, which you can check using the URL http: // localhost: 8080 / calculator? Wsdl in your browser.

+1
Sep 20 '15 at 8:38
source share



All Articles