Java version compatibility when working with different api in different version for same task

StackOverflow https://stackoverflow.com/questions/10430526

Domanda

i wrote a applet for screen Capture for jre 1.7, jre 1.6 and jre 1.5. The applet has to use transparent background for it's window. But for translucency jre 1.7(graphicDevice window Translucency) and 1.6(awtutilities) uses different api and there is work around for jre 1.5. Now how to make my applet compatible for all the three version?

I guess i have to compile different classes with different compilers. But how use these separately compiled classes in a single application?

È stato utile?

Soluzione 2

If there are different APIs present in different versions of java for the same functionality(as JWindow.setOpacity() in java 1.7 and AWTUtilities.setWindowOpacity() in java 1.6), then we can use dynamic loading of classes to use the APIs depending on the availability of class w.r.t java version. Here is the code that solved my case :

    try
    {
        Class<?> cls = Class.forName("javax.swing.JWindow");
        Method meth = cls.getMethod("setOpacity", float.class);
        meth.invoke(transparentWindow, 0.50f);
    }
    catch (Throwable e)
    {
        e.printStackTrace();
        try
        {
            Class<?> cls = Class.forName("com.sun.awt.AWTUtilities");
            Method meth = cls.getMethod("setWindowOpacity", Window.class,
                                        float.class);
            meth.invoke(null, transparentWindow, 0.50f);
        }
        catch (Throwable e1)
        {
            e1.printStackTrace();
        }
    }

Hope it helps to beginners like once I was :)

Altri suggerimenti

According to the referred article..

OK, I feel I should drop in at this stage since I up-voted the comment of @Alexie ..and wrote the article and applet. ;)

1st up. I had largely forgotten that applet and was going to suggest JWS/embedded applet, but that would only work for 1.6_10+ (that is when Sun released the Plug-In 2 JRE - different for other JRE makers).

Then when I reviewed the applet, it became apparent that Alexie had nailed it. With a few pages, some version specific Jars and a little 'jumping through hoops', it can fulfill the spec.

Here is how it would work.

  • Have the 'public link' pointing to the 1.7+ version of the applet (for instance called: applet.html, using archive="appletcommon.jar,applet7.jar"). Use the Java Version Checker Applet to check for 1.7 (or beyond). If not available:
    • Redirect to applet6.html, for the 1.6 archive="appletcommon.jar,applet6.jar". If not available:
      • Redirect to applet5.html, for the 1.5 archive="appletcommon.jar". If not available:
        • Redirect to a page that states "Sorry, need a 1.4+ plug-in, see a, b, c for install". You might use the deployJava.js here to ask for '1.2+ Java' - Oracle would supply the latest publicly released version - I'm not sure about Apple or *nix, but would expect the same.

The important thing here is the fine description of the content of the various archives. That breaks down to.

  • appletcommon.jar The core applet classes that are compatible with the minimum version for which this applet is available (in this case - 1.5). -source/target during compilation must==1.5, and
  • applet6.jar with 1.6 classes. Ditto the compilation options.
  • applet7.jar with 1.7 classes. Ditto the compilation options (unless compiling with a 1.7 SDK).
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top