Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > compile directive for conditional compile for Java 1.4 versus Java 5

Reply
Thread Tools

compile directive for conditional compile for Java 1.4 versus Java 5

 
 
timjowers
Guest
Posts: n/a
 
      07-02-2007

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();
}
}
}

 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      07-02-2007

timjowers wrote:
>
> 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.
>


<http://www.google.com/search?q=java+conditional+compilation&hl=en&start= 10&sa=N>


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-02-2007
On Mon, 02 Jul 2007 18:11:21 -0000, timjowers <>
wrote, quoted or indirectly quoted someone who said :

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


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

see http://mindprod.com/jgloss/properties.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      07-03-2007
timjowers wrote:
> 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
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      07-03-2007
Roedy Green wrote:
> On Mon, 02 Jul 2007 18:11:21 -0000, timjowers <>
> wrote, quoted or indirectly quoted someone who said :
>
>> long start = System.currentTimeMillis(); // only compile this line
>> in Java 1.4
>> //long start = System.nanoTime(); // only compile this line in Java

>
> 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
 
Reply With Quote
 
timjowers
Guest
Posts: n/a
 
      07-03-2007
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

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      07-04-2007
timjowers wrote:
> 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
 
Reply With Quote
 
heyjude heyjude is offline
Junior Member
Join Date: Feb 2011
Posts: 1
 
      02-02-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
Conditional directive Simon ASP .Net 2 11-25-2005 05:18 PM
Conditional Directive RC ASP .Net 1 05-16-2005 10:52 AM
The 'Location' attribute is not supported by the 'OutPutCache' directive????when i set in the directive .NET Follower ASP .Net 1 02-13-2004 10:32 AM



Advertisments