We help IT Professionals succeed at work.

Determine JDK version during runtime and Compilation

kmenzel
kmenzel asked
on
Medium Priority
783 Views
Last Modified: 2008-03-17
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.
Comment
Watch Question

Commented:
If you're looking for conditional compilation in Java, here's an article and code that might interest you:

http://www.unclechicken.com/labnotes/flabnote.html
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.

Commented:
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.

Author

Commented:
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

Commented:
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.

Commented:
Refer "java.version" system property.
You can get JavaVM version with this property.

<<code>>
String value = System.getProperty("java.version");
CERTIFIED EXPERT

Commented:
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

Commented:
Well, that article describes what mzimmer74 had said.

Author

Commented:
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.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.