Search code examples
javascriptmozillajpm

Execute .jar file through Mozilla Add-on


I have been trying to find a way to execute a .jar file using Mozilla JPM (I am well aware that an add-on that accomplishes this exists but I want to learn). I followed the online tutorial and this is my code right now

var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");

var button = buttons.ActionButton({
  id: "mozilla-link",
  label: "Visit Mozilla",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
 onClick: handleClick
});

function handleClick(state) {
  tabs.open("C:\Users\QaziWa\Documents\Jar\DownloadReportPPE.jar");
}

Now I have successfully opened URLs but I cannot find any documentation on how to execute a jar file. My code above:

tabs.open("C:\Users\QaziWa\Documents\Jar\DownloadReportPPE.jar");

is clearly incorrect.

I was looking at this link: https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs, but I failed to find anything that presents the ability to execute a jar file.

My aim is to create an add-on button that will execute this jar file for me. This jar file downloads pdf reports on a website so I would like it to execute in a new tab or window. What method will give me what I am looking for or is there any documentation that can help me.


Solution

  • Most likely not the best solution but I figured out something that works.

    The following code will execute a .jar file by clicking the Mozilla add-on button:

    var buttons = require('sdk/ui/button/action');
    
    var button = buttons.ActionButton({
      id: "execute-jar",
      label: "Download Report",
      icon: {
        "16": "./icon-16.png",
        "32": "./icon-32.png",
        "64": "./icon-64.png"
        //Icons for the button
      },
      onClick: handleClick
    });
    
    
    
    function handleClick(state) {   
    
        let {Cc, Ci} = require('chrome');
        var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
        file.initWithPath("C:\\Users\\QaziWa\\DownloadReportPPE.jar");
    
        if(file.exists()){
            file.reveal();
            file.launch();
        }
        else {
            console.log('Failed.');
        }
    }