• Post Reply Bookmark Topic Watch Topic
  • New Topic

Different Code For Different JVM Versions  RSS feed

 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm sure this will have been discussed before, but can't find anything along the same lines - apologies if it's repetitive.

I'm wanting to use a feature in Java 1.6 (Window.setIconImages(List)), which isn't in 1.4 or 1.5, and I want to be able to run the software on both of these JVMs.

So, I have something along these lines:



The 'if' section is 1.6 friendly, the 'else' works in both 1.4 and 1.5.

Obviously this compiles and runs fine under 1.6, but running it in 1.4 or 1.5 JVMs will throw an error.

So, my question is: what can I do? Is there some way I can tell the JVM to ignore a specific line if it doesn't like it, or do I need to do completely separate builds?

One thought I have is that, at the moment, the code is in my main class, so the software falls over as soon as it runs, with an 'Unresolved compilation problem' (should mention that this is all testing in Eclipse at the moment, I haven't had a chance to try it on a 'real' system). Perhaps if I put the code in a separate class, and call it from within the if statement, it won't matter that that class didn't compile, if I never actually use it. That feels a bit wobbly, but could work?
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

If your requirement is to tell the JVM to omit some of the codes if it does not comply to it. My idea is you can very well get the Version of the current JVM using System properties.



Using that String you can determine whether a block of code should be executed or not.

regards,
dinesh.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's basically the trick, yes; I've done similar things. As long as you refer to a class only in the body of a method, then the class won't be loaded unless the method is called. You could even use Class.forName() to load a class by name, further isolating your main code from your JVM-version-specific code.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you can even go a step further and only put the instanciation of the correct class into the if statement. The method call then can happen once, polymorphically.

That's basically an application of the Strategy design pattern.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses, guys.

dinesh, I think you might have misunderstood, the if statement is fine, it's how I avoid the fact that the 1.6-only code won't work in 1.4 or 1.5.

Ernest and Ilja, thanks - your answers make good sense, and I'll give them a go. Ilja, are you suggesting something like:



That's a very vague example - obviously won't work, but is that your reference to polymorphism, or is there something cleverer that I've missed?

Again - thanks, saved me much messing around!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The polymorphism angle would be something like this: say that SetIconNewVersion and SetIconOldVersion are classes that implement an interface SetIcon. Then you can say

SetIcon iconSetter = (SetIcon) Class.forName(classToLoad).newInstance();

and then the rest of the code can use iconSetter (polymorphically) regardless of its implementation.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably try without Class.forName first, along the lines of

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to use the reflection libraries for the method call. This way, you don't have to setup an interface and different class versions, just for a simple method call. Something like this...



It's a bit awkward, but it should work for Java 6. And as for Java 1.4 and 5, it should simply not find the method -- and not make the call.

Henry
[ January 15, 2007: Message edited by: Henry Wong ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I was under the impression that if the class showed up in the class file its definition could be loaded at any point even if the method that uses it is not called. If that is not the case then I got some optimization I need to do
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that yes,, a class may be loaded before a method that uses it is called. The specs are somewhat flexible about when class loading occurs. "In particular, a class loader may cache binary representations of classes and interfaces, prefetch them based on expected usage, or load a group of related classes together." (JLS 12.2.1) It's class initialization that is required to be performed immediately before certain events (JLS 12.4.1) I think that typically class loading occurs right before initialization, but I don't know how commonly there may be exceptions to this. I think if I wanted to be sure a class wasn't loaded too soon (with the wrong loader), I would put the class loading behind Class.forName() as an added guard.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, I used the full, all-singing, all-dancing, polymorphic Class.forName() option - 82 lines of code to set the icon on the title bar... Works perfectly though!
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry - one last question, is there any easy way to tell Eclipse to ignore errors in the 1.6 version class file, if I'm using the 1.4 JVM? It really doesn't matter, but the red 'x' and the 'Errors exist... ' warning are upsetting me...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!