45

I want to compile my .java's (several) into one .jar that are compatible with at least Java 1.6 and newer, preferably Java 1.5 and newer versions of Java. (I have Java 1.7.0_5)

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
Primm
  • 1,347
  • 4
  • 19
  • 32

6 Answers6

29

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.

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • It might be however easier to use Java 1.4 Compiler from the beginning. – eckes Oct 19 '18 at 21:26
  • 5
    It seems one cannot set a `-source` version higher than the `-target` version. `$javac abc/def/HelloDriver.java -source 1.7 -target 1.4` `javac: source release 1.7 requires target release 1.7` – flow2k Feb 20 '19 at 22:50
29

As of JDK 9, javac support a new option for cross-compiling

javac --release N ...

which is equivalent to

javac -source N -target N –bootclasspath rtN.jar
ruvim
  • 7,151
  • 2
  • 27
  • 36
Joe Darcy
  • 331
  • 3
  • 2
  • 3
    Compiling code for java 8 using the JDK 10 I ran the command `javac --release 8 Main.java ` The result was runnable on java 8. – ug_ May 11 '18 at 20:09
  • Doesn't require an old version of JDK, compiles back as far as version 6 (JDK 10), and only the Unicode version is pulled from the more recent platform. – veganaiZe May 14 '18 at 16:26
  • `Old` does not mean bad, especially when `new` means paid or limited features. Also incompatibility with `stable` and `free` does not mean good. – user1742529 Jun 17 '19 at 23:26
3

You can use javac -target 1.5 <source files>.

If you're using a build system, Eclipse or some other IDE to build jars, please specify which one.

cha0site
  • 10,517
  • 3
  • 33
  • 51
  • i have netbeans, but i can just use text files. How would i apply that into all .java's in a directory? – Primm Jul 06 '12 at 15:06
  • 1
    With netbeans right click on your project, go to properties and change the "Source/Binary format" from the dropdown towards the bottom of the dialog that opens – tofarr Jul 06 '12 at 15:09
1

You can specify the target version of the compiler lower than the build java version (all answers basically said that) but you should not: it is quite messy as it only works if you also supply the class libraries for the actual target version. If you do that you are much better off with simply using this old Java version to compile. Much cleaner and less stuff to setup. For Java 9 the options are a bit nicer but you still need an old JDK lying around for the rt.jar.

What you can do is in your ide and dedvelopment cycle use the -target/-bootclasspath trick to get faster compiler and better warnings, but IT and release builds then should be done with the target version.

eckes
  • 10,103
  • 1
  • 59
  • 71
0

You can use the newest version of the JDK and set the source level to 1.5 - as long as you don't use classes / methods that were introduced after 1.5 you should be ok for all versions of the JVM 1.5 and above:

-source 1.5

tofarr
  • 7,682
  • 5
  • 22
  • 30
0

The following command will compile all your java classes with java version 1.8 because by default your code compiled by the latest version which you have.

javac -source 1.8 -target 1.8  *.java
INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63