Switch gadget between Java versions

    Greetings, harazhiteli!

    Today I want to touch on the topic of minor refinement of my virtual workplace. I ask you not to place high hopes on this note, since after reading it, the code will still not be written by itself, but we will still overcome a bit of routine in the development. Plus try yourself in writing gadgets for Windows.

    And let's start from the end and take a look at the finished result:


    I will go to the point. When developing for the Java platform, I am not limited to one version of it. There are many reasons for this: the target hardware platform, the presence of legacy, etc. ... And in this regard, I often have to correct the environment variables JAVA_HOME / JRE_HOME / PATH.

    Hence the idea to implement a switch, which will conveniently automate these actions. But how to bring the idea to life, so that it is convenient? I didn’t have to think about it for a long time, the format of the target application became clear immediately after the look fell on the weather widget on the desktop. So, this will be the windows sidebar gadget.

    For the impatient, let's try:
    1) In the user environment variables you need to create: JAVA_HOME and JRE_HOME
    2) Add% JAVA_HOME% \ bin to PATH
    3) Download and install the gadget - github.com/ice-pro/JavaEnvSwitchGadget/blob/master/JavaEnvSwitchGadget .gadget
    4) But in order for everything to work as it should, I had to take another very non-standard step: java.exe already lies in windows / system32 and the priority of this folder is higher than what is specified in PATH. Therefore, I had to rename the file from system32 to "_java.exe".

    Everything, you can configure the paths to the installed JDK / JRE and use it.

    And now for those who are interested in details.
    What is a gadget? Gadget in windows is an application with which you can visualize some data or even carry out feedback.

    Gadget development is based on a standard set of web technologies - HTML / CSS / JS. Of course, you can use C # (for example, using this template here - visualstudiogallery.msdn.microsoft.com/bf347eb6-99bd-4c99-89d0-6ca3fe1eb54e), but in this article I will not complicate simple things and choose the native path.

    The basic framework of the gadget consists of two files - an html page with a visual part and an xml descriptor.
    A full description of the descriptor tags can be found here msdn.microsoft.com/en-us/library/windows/desktop/ff486356 (v = vs.85) .aspx
    Descriptor for my gadget (I will not give the code, since there is nothing remarkable there) , along with the rest of the source code is available here github.com/ice-pro/JavaEnvSwitchGadget

    Now let's move on to the main page of the gadget. There are several points that I want to clarify:
    - because the gadget will contain another additional page - settings, then you need to specify it explicitly in this way (for me the fact that this was not necessary in xml was slightly strange):

    document.onreadystatechange = function() {
        if(document.readyState == 'complete') {
            System.Gadget.settingsUI = 'settings.html';

    - to work with the setup, a simple API is used:
    var val = System.Gadget.Settings.read(key);

    - and the most interesting thing is working with environment variables:
    var shell = new ActiveXObject('WScript.Shell');
    var vars = shell.Environment('User');
    vars('JAVA_HOME') = jdk;
    vars('JRE_HOME') = jre;

    There is only one place on the parameters page that I would like to focus on - saving these very parameters:

    System.Gadget.onSettingsClosing = applySettings; // указываем функцию которую нужно вызывать при закрытии окна параметров
    function applySettings(event) {
        if (event.closeAction == event.Action.commit) {
            // сохраняемся...

    That's all, the implementation is ready. Now you can start assembling. The finished gadget is a zip archive, only with the extension .gadget.

    For the assembly, I wrote a small command script. It looks like this:

    del JavaEnvSwitchGadget.gadget
    "C:\Program Files\7-Zip\7z.exe" a -tzip -x!.idea -x!.git -x!_stuff -x!build.cmd -r JavaEnvSwitchGadget *.*
    move JavaEnvSwitchGadget.zip JavaEnvSwitchGadget.gadget

    And now a little about debugging (you can read in detail here msdn.microsoft.com/en-us/library/windows/desktop/bb456467 (v = vs.85) .aspx)

    To start with the bad news: quickly debug with alert- It will not work, as there is no implementation in Sidebar (workaround - use MsgBox).

    But let's do it right.

    1) First of all, configure Internet Explorer, in the Advanced section, uncheck the box next to "Disable script debugging"
    2) Set up Visual Studio, in Tools - Options - Just-In-Time put a daw next to Script.
    3) Last, to interrupt the work of the gadget and call the debugger for help, you need only one line in the code


    Now, when you start the gadget, we will see a window prompting us to connect to the code trace.

    Thanks for reading, I hope someone comes in handy.

    Useful links:
    1) github.com/ice-pro/JavaEnvSwitchGadget - sources and assembly
    2) msdn.microsoft.com/en-us/library/windows/desktop/bb456468 (v = vs.85) .aspx - a step-by-step guide for development
    3) msdn.microsoft.com/en-us/library/windows/desktop/bb456467 (v = vs. 85) .aspx - description of the debugging method
    4) msdn.microsoft.com/en-us/library/windows/desktop / ff486356 (v = vs. 85) .aspx - description of the xml descriptor
    5) msdn.microsoft.com/en-us/magazine/cc163370.aspx- another article about development, but older for Vista
    6) stackoverflow.com/questions/768477/setting-an-environment-variable-in-javascript - working with environment variables from JS

    PS Concerning the versions between which switching is proposed , I, perhaps, will not say anything. If anyone needs to trim / expand the list, then this is done quickly.
    PS2. By analogy (yes, copy-paste), you can implement a switch for Python 2/3.

    Also popular now: