使用firefox启动在系统PATH中找到的外部程序

问题描述

|| 我有一个插件,我想用来启动计算机上已经安装的程序,例如notepad.exe 问题是,Firefox似乎没有在系统PATH中搜索该程序,即
        file.initWithPath(\"c:\\\\windows\\\\system32\\\\notepad.exe\");//works
    //file.initWithPath(\"notepad.exe\");//does not work
        //file.initWithPath(\"%systemroot%\\\\notepad.exe\");//does not work
题: 有没有办法让Firefox在系统PATH中查找程序? 这是我的完整功能
autoStartNotepad:function()
{
    // create an nsILocalFile for the executable
    var file = Components.classes[\"@mozilla.org/file/local;1\"]
         .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath(\"notepad.exe\");//does not work
    //but file.initWithPath(\"c:\\\\windows\\\\system32\\\\notepad.exe\");//works

    // create an nsIProcess
    var process = Components.classes[\"@mozilla.org/process/util;1\"]
                        .createInstance(Components.interfaces.nsIProcess);
    process.init(file);

    // Run the process.
    // If first param is true,calling thread will be blocked until
    // called process terminates.
    // Second and third params are used to pass command-line arguments
    // to the process.
    var args = [];
    process.run(false,args,args.length);
}
    

解决方法

        
nsIProcess
不会这样做,它只接受完整路径。您需要亲自查看环境变量:
var environment = Components.classes[\"@mozilla.org/process/environment;1\"]
                            .getService(Components.interfaces.nsIEnvironment);
var path = environment.get(\"PATH\");
var root = environment.get(\"SYSTEMROOT\");
您可以拆分
PATH
变量并检查各个目录,也可以使用
SYSTEMROOT
变量的值。 您可以运行
cmd.exe /c notepad.exe
,但也需要首先定位
cmd.exe
。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...