Link to home
Start Free TrialLog in
Avatar of kmenzel
kmenzel

asked on

Determine JDK version during runtime and Compilation

I have an app that needs to run and compile with 1.1.7 and 1.2 and beyond. I would like to use some features of 1.2+ when I am running with it.

Is there any way to detect what version of the compiler is being used when the code is compiled?

Basically, I'm looking for C/C++ type compiler directives like
#ifdef 1.2+
// do some 1.2 stuff
#else
// do 1.1.7 instead
#endif

There isn't such a thing, but are there any other alternatives?

Thanks for the help.
ASKER CERTIFIED SOLUTION
Avatar of yongsing
yongsing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mzimmer74
mzimmer74

I just read the paper that yongsing suggested, and it would probably meet the thing you are wishing to do.  However, in my opinion there is a MUCH simpler way of doing this.  Define a variable like this in some class A:

public final static boolean VERSION = true;

now, say true is 1.1.7 and false is 1.2 (or the other way, really doesn't matter).

Then, around the code where you would normally use a #ifdef you can simply put the following:

if (A.VERSION)
{
  //1.1.7 code
}
else
{
  //1.2 code
}


Due to the way java compiles classes, if VERSION is true the else statement will be compiled away, otherwise the if portion of the code will be compiled away.

Just another possibility for you.
another option, is to compile it all always, and detect what to use at runtime using system properties.
if you will give more details about the specific problem you have, I may be able to help you further.
Avatar of kmenzel

ASKER

I'm using Visual Cafe 4.5 and it has an internal VM that is 1.1.7. If I want to be able to visually edit my classes for screen layout, my code must be compatible with 1.1.7.

Most of my screen classes inherit from a base class that uses some 1.2+ code (awt.print and Comparable for instance). So I've been commenting out the 1.2+ code whenever I need to edit visually and uncomment it when I am ready to run the app. Pain in the ASS. VC really should support 1.2 but they still don't in their most recent version.

So, I was hoping there would be some slick way to tell what JDK version was compiling the code and hide the 1.2 stuff when switching to 1.1.7 for visually editing.

Sounds like youngsing's solution is best for now even though it still requires some work.

Any better ideas would be appreciated.

Thanks
look, generaly your IDE should not limit you.
if you waste time fighting with the IDE its time to find a new one.
JBuilder supports 1.2 code (even 1.3), and allow visual editing.
maybe you should try it.
Refer "java.version" system property.
You can get JavaVM version with this property.

<<code>>
String value = System.getProperty("java.version");
Avatar of Valeri
In my opinion it's a better article for conditional compilation in JAVA :
http://www.javaworld.com/javaworld/javatips/jw-javatip5.html

Best Regards,
Valeri
Well, that article describes what mzimmer74 had said.
Avatar of kmenzel

ASKER

Thanks for the help. It's not nearly as good as C #ifdef but helps some. Sorry it took so long to grade. I've been working on other projects.