I am trying to write a modification to MapTool (for my own purpose and anyone else that likes the mod) which is based on Java 7's nio function. However, for users who are not using Java 7, I can provide similar (but slightly watered down) functionality using pre Java 7 io functions.
It was suggested to me to Class to try to call the Java 7 Class and if this fails resort to using the Java 6 version. I found the following existing example in MapTool.java:
Code: Select all
try {
Class<?> appClass = Class.forName("com.apple.eawt.Application");
Method getApplication = appClass.getDeclaredMethod("getApplication", (Class[]) null);
Object appl = getApplication.invoke(null, (Object[]) null);
Method setDockIconImage = appl.getClass().getDeclaredMethod("setDockIconImage", new Class[] { java.awt.Image.class });
// If we couldn't grab the image for some reason, don't set the dock bar icon! Duh!
if (img != null)
setDockIconImage.invoke(appl, new Object[] { img });
if (MapToolUtil.isDebugEnabled()) {
// For some reason Mac users don't like the dock badge icon. But from a development standpoint I like seeing the
// version number in the dock bar. So we'll only include it when running with MAPTOOL_DEV on the command line.
Method setDockIconBadge = appl.getClass().getDeclaredMethod("setDockIconBadge", new Class[] { java.lang.String.class });
String vers = getVersion();
vers = vers.substring(vers.length() - 2);
vers = vers.replaceAll("[^0-9]", "0"); // Convert all non-digits to zeroes
setDockIconBadge.invoke(appl, new Object[] { vers });
}
} catch (Exception e) {
log.info("Cannot find/invoke methods on com.apple.eawt.Application; use -X command line options to set dock bar attributes", e);
}
If I am correct the relevant part to invoking the class (and thus determining if it is available) can be summarized with:
Code: Select all
try {
Class<?> appClass = Class.forName("[i]file_in_which_the_class_is_defined[/i]");
Method getApplication = appClass.getDeclaredMethod("[i]class_name[/i]", (Class[]) null);
Object appl = getApplication.invoke(null, (Object[]) null);
} catch (Exception e) {
// Class was not loaded
}
Code: Select all
// Clean up the cache
try
{
Class<?> java7CacheClass = Class.forName("net.rptools.maptool.client.CacheCleanerJava7");
Method getJava7CacheApplication = java7CacheClass.getDeclaredMethod("CheckCacheSize", (Class[]) null);
getJava7CacheApplication.invoke(null, (Object[]) null);
}
catch(Exception x)
{
try
{
Class<?> java6CacheClass = Class.forName("net.rptools.maptool.client.CacheCleanerJava6");
Method getJava6CacheApplication = java6CacheClass.getDeclaredMethod("CheckCacheSize", (Class[]) null);
getJava6CacheApplication.invoke(null, (Object[]) null);
}
catch(Exception x2)
{
MapTool.showWarning("Unable To Load Java6- or Java7+ CacheCleaner Implementation.");
}
}
When I try this on my Win8 machine using Java 7 it correctly runs the Java 7 branch of the code. However, when I try the same code on my Ubuntu machine running Java 6 then the program cores before it gets to the Java 6 branch. I believe that my Ubuntu Java 6 machine is set up correctly because it can run an unmodified version of MapTool without any problems.
As far as I know the Java 7 Class uses nio and thus is not pre Java 7 compatible but the Java 6 Class code does not use any Java 7 functions and thus should be Java 6 compatible.
I am enclosing a Diff Path for my modifications which includes the source code for the two (Java 6 and Java 7) new classes.
Can anyone point me in the right direction as to why my Java 6 / Java 7 compatibility does not seem to be working?