1

I intend to write private-use command line tools in java for use both on my private system (jdk 7) and on my universities number-crunching servers (jre 1.4). An attempt at installing OpenJdk from sources failed, because it is missing several dependencies, that simply wouldn't make sense on a computation server -- e.g. CUPS. The work required for installing such dependencies and THEIR dependencies would probably defeat the whole point of automating tasks, i.e. making life easier.

Being used to the conveniences of generics, I don't want to write 1.4 SOURCE code though. I found that, when compiling from the command line, options like

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

are available (see [1]), which should allow compiling jdk5 or even jdk7 specific syntax to jdk1.4 compatible bytecode, as long as I stay clear of newer library features (which -bootclasspath is for).

This brings up two problems:

  1. While I can set the compliance level for each project to a given java version easily in Netbeans 7.3, it forces me to use 1.4 syntax as well (probably by adding -source 1.4 to the command above).

  2. I couldn't find an old jdk (specifically classes.zip) anywhere except for oracle.com, where registration is required for downloading those with the registration mask alone making quite clear that those are not meant for private use.

All related answers I found so far give no hint how to do this kind of bytecode/source-separated cross-compilation in netbeans and none address the issue of finding old JDKs.

Eclipse is not really an option, as there I couldn't figure out how to get automatic generation of JARs like in netbeans.

Any ideas?

Platform details:

  • Local (from Netbeans "Help → About"):

    Product Version: NetBeans IDE 7.3 (Build 201302132200)
    Java: 1.7.0_11; Java HotSpot(TM) 64-Bit Server VM 23.6-b04
    Runtime: Java(TM) SE Runtime Environment 1.7.0_11-b21
    System: Windows 7 version 6.1 running on amd64; Cp1252; de_DE (nb)
    
  • Remote:

    java version "1.4.2_11"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_11-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 1.4.2_11-b06, mixed mode)
    

Update: Just to be sure -- it IS possible to compile source with new syntax to Java 1.4. E.g. I wrote this file:

class Target<T>{
    public T field;

    public static void main(String[] args){
        System.out.println("Hello World!");
        Target<String> target = new Target<>();
        target.field = "More Worlds.";
        System.out.println(target.field);
    }
}

Then I compiled it with JDK 7 doing

javac -target jsr14 Target.java

and uploaded it to the computation server, where only JRE 1.4 (and no JDK at all) is present. It gave the expected output

Hello World!
More Worlds.

Apparently the "jsr" targets are an undocumented feature though, see e.g. [2]. Also that link mentions, that it is a bit of a hack, as only for-each loops for the Collections library will be handled:

for-each loop: When iterating over an array, the compiler generates an induction variable and the standard array iteration idiom. When iterating over a Collection, the compiler generates the standard iterator-based idiom. When iterating over a non-Collection Iterable, the compiler produces an error.

I guess that means, that I will have no choice but to attempt getting a newer JRE onto the server, if I want to use any reasonably modern features...

[1] How do i compile a .java with support for older versions of java?
[2] http://twit88.com/blog/2008/08/26/java-understanding-jsr14/

Community
  • 1
  • 1
kdb
  • 4,098
  • 26
  • 49

1 Answers1

1

JDKs aren't forward-compatible. That is, you cannot compile new features for old JDK versions. (or JRE for that matter). Language constructs, such as generics, are if I'm not totally mistaken, part of the bytecode or interpreter in Java.

AFAIK you have only two options. 1) install new JDK or 2) write 1.4 source code.

fredrik
  • 6,483
  • 3
  • 35
  • 45