compile directive for conditional compile for Java 1.4 versus Java 5

T

timjowers

Does a way exist to instruct the Java compiler to compile code only
if a certain version of Java is supported? For example, I may want to
measure with milliseconds in Java 1.4 or nanoseconds if I'm running in
Java 5.

import java.io.IOException;

public class JVMLoadTimeRunner {

public static void main(String[] args) {

long start = System.currentTimeMillis(); // only compile this line
in Java 1.4
//long start = System.nanoTime(); // only compile this line in Java
5

try {
Process newP = Runtime.getRuntime().exec( cmd );
newP.waitFor();
long end = System.currentTimeMillis();
System.out.println( "Process Execution took " + (float)(end-start)/
1000F + " seconds" );
// Java 5
// System.out.println( "Process Execution took " + (float)(end-
start)/1000000000F + " seconds" );
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println( "Process wait threw an Exception" );
e.printStackTrace();
}
}
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

timjowers said:
Does a way exist to instruct the Java compiler to compile code only
if a certain version of Java is supported? For example, I may want to
measure with milliseconds in Java 1.4 or nanoseconds if I'm running in
Java 5.

Not as you can in C/C++.

You could use an external preprocessor.

But I would drop the idea.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Roedy said:
You can look at a system property at run time and use that in an if.

java.specification.version = 1.6

java.version = 1.6.0_01

java.vm.version = 1.6.0_01-b06

That will not help at compilation.

Arne
 
T

timjowers

FWIW, I decided on the exploit of compiler optimization. The technique
is to create unreachable code with as if(false) and put your Java 5
code there. Make that block reachable to compile in Java 5. E.g.

boolean JAVA_5_PLUS = false;
// Java by design lacks conditional compilation so various
workarounds
// are to comment out code, use a custom preprocessor (see Munge
from the Swing team),
// or use if( false ) or such blocks (which are normally not
compiled due to optimizing them out.)
if( JAVA_5_PLUS )
start = System.nanoTime();
else
start = System.currentTimeMillis();
TimJowers
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

timjowers said:
FWIW, I decided on the exploit of compiler optimization. The technique
is to create unreachable code with as if(false) and put your Java 5
code there. Make that block reachable to compile in Java 5. E.g.

boolean JAVA_5_PLUS = false;
// Java by design lacks conditional compilation so various
workarounds
// are to comment out code, use a custom preprocessor (see Munge
from the Swing team),
// or use if( false ) or such blocks (which are normally not
compiled due to optimizing them out.)
if( JAVA_5_PLUS )
start = System.nanoTime();
else
start = System.currentTimeMillis();

I am very surprised if that compiles with the 1.4 compiler.

Compile with 1.5 and run on 1.4 should work.

Arne
 
Joined
Feb 2, 2011
Messages
1
Reaction score
0
check out the wwyt conditional compilation pre-processor

There is an external tool called wwyt which does just that. It's a Windows command line tool, it pre-process your Java source files, which are embedded with conditional compile directives (like #if, #else, ...). You then let compiler compile these converted files and get the .class files you want. After compilation, you should run wwyt tool again so that the converted files are restored back to their original status.

URL is this: adarian dot com slash wwyt

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top