Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Forwards compatible program possible?

Thread view: 
jonasforssell@yahoo.se - 25 Jul 2006 17:10 GMT
Hello Experts,

The 1.4 API has a few very useful functions like
Runnable.getNumberOfProcessors()

Is it possible to write a program which uses these if possible but does
not demand a 1.4 JRE and disables the feature if run on an 1.2 JRE?

If so, how would an example look like?

Thanks
/Jonas Forssell, Gothenburg, Sweden
Patricia Shanahan - 25 Jul 2006 18:46 GMT
> Hello Experts,
>
[quoted text clipped - 8 lines]
> Thanks
> /Jonas Forssell, Gothenburg, Sweden

How about reflection?

      try {
        Method m = Runtime.class.getDeclaredMethod(
            "availableProcessors",new Class[0]);
        Object o = m.invoke(Runtime.getRuntime(),new Object[0]);
        return ((Integer)o).intValue();
      } catch (NoSuchMethodException e) {
        return 1;
      }

You will need more exception handling. The NoSuchMethodException block
should return whatever number of processors you want to assume in the
1.2 JRE.

Patricia
jonasforssell@yahoo.se - 25 Jul 2006 21:28 GMT
Patricia Shanahan skrev:

> > Hello Experts,
> >
[quoted text clipped - 25 lines]
>
> Patricia

Excellent proposal.
Thanks for your help!
/jonas
Jean-Francois Briere - 25 Jul 2006 22:01 GMT
To avoid reflection you could write your 1.4+ specific code in an
helper/utility class.
Each method would have to do try { } catch (NoSuchMethodError e) { } in
its body.
Then compile this class with a 1.4+ compiler with flags: -source 1.2
-target 1.2

public class Utils {
   public static int getNbrOfProcs() {
       try {
           // 1.4+ code
           return Runtime.getRuntime().availableProcessors();
       }
       catch (NoSuchMethodError e) {
           // Maybe print something
           return 1;
       }    
   }
}

Regards
Thomas Hawtin - 26 Jul 2006 15:01 GMT
> To avoid reflection you could write your 1.4+ specific code in an
> helper/utility class.
[quoted text clipped - 15 lines]
>     }
> }

Dangerous thing to do. The point the exception is thrown from is not
always obvious, for frequently executed code it can be slow and it does
not clearly distinguish between 1.2 and 1.4 code. There's a piece on it
in Java Puzzlers.

My preferred approach allows the 1.2 code to be compiled against the 1.2
rt.jar. Make a class of 1.4 specific stuff that implements an 1.2
compatible interface. Compile the 1.4 class with -target 1.4 and use the
static initialiser to check that it really is running on 1.4+. The 1.2
code can be compiled against the 1.2 rt.jar and use Class.forName to
load the 1.4 specific stuff (once).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Jean-Francois Briere - 26 Jul 2006 20:43 GMT
> Dangerous thing to do.

Not really

> The point the exception is thrown from is not always obvious,

Yes it is for well documented methods .

> for frequently executed code it can be slow

Agreed

> and it does not clearly distinguish between 1.2 and 1.4 code.

Yes it does for well documented methods .

> My preferred approach ...

I like this approach. In fact this is a cleaner (using interfaces and a
version factory approach) and more efficient approach.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.