Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to Java 1.6 APIs while degrading gracefully against Java 1.5?

I would like to use the java.text.Normalizer class from Java 1.6 to do Unicode normalization, but my code has to be able to run on Java 1.5.

I don't mind if the code running on 1.5 doesn't do normalization, but I don't want it to give NoClassDefFoundErrors or ClassNotFoundExceptions when it runs.

What's the best way to achieve this?

like image 566
Simon Nickerson Avatar asked Aug 14 '09 11:08

Simon Nickerson


1 Answers

The usual way of doing this is via reflection, i.e. don't refer directly to the class in question, but invoke it programmatically. This allows you to catch exceptions gracefully if the code in question doesn't exist, and either ignore it, or try something else. Reflection throws ClassNotFoundException, which is a nice normal exception, rather than NoClassDefFoundError, which is a bit scarier.

In the case of java.text.Normalizer, this should be pretty easy, since it's just a couple of static methods, and easy to invoke via reflection.

like image 91
skaffman Avatar answered Sep 27 '22 18:09

skaffman