如何启动仅使用VBScript提升的java程序(来自.jar)

时间:2015-05-07 10:05:39

标签: vbscript jar uac

有一个great answer providing a batch file总是会做什么,最好是提升,如果已经升高则不会升高。

我不想用我的程序分发批处理文件。答案的核心是这个VBSScript:

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "[path to the batch file which will run elevated]", "ELEV", "", "runas", 1 

非常简单。因此,我只想使用jar文件的路径而不是批处理文件的路径。但它似乎不起作用:

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "AutoClient.jar", "ELEV", "", "runas", 1 

**Error:** There is no program associated with this file to perform the operation. Install a program or, if already installed one, create an association control panel "preselectable programs. "

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "javaw -jar AutoClient.jar", "ELEV", "", "runas", 1 

Windows cannot find javaw -jar AutoClient.jar. Make sure you typed the name correctly.

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "javaw", "ELEV", "-jar AutoClient.jar", "runas", 1 

image description

那么,如何从vbs文件中运行jar?两个文件共享同一目录。 java应用程序的工作目录是那个目录是必要的。

修改 所以,感谢@MCND(和this)我现在知道参数如下:

path to executable to run
command line parameters sent to the program
working directory of the new process
'runas' command which invokes elevation
0 means do not show the window, 1 to show the window

感谢他的代码:

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "javaw.exe", "-jar AutoClient.jar", "", "runas", 1 

我可以在我的收藏中添加另一个错误:

image description

3 个答案:

答案 0 :(得分:0)

documentation表示调用中的第一个参数是要启动的文件,将参数留给第二个参数。所以它应该是(抱歉,未经测试)

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "javaw.exe", "-jar AutoClient.jar", "", "runas", 1 

答案 1 :(得分:0)

到目前为止,我没有发生疯狂弹出错误的唯一方法就是:

' Get the script location, the directorry where it's running
Set objShell = CreateObject("Wscript.Shell")

strPath = Wscript.ScriptFullName

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

' Args:
'   path to executable to run
'   command line parameters - first parameter of this file, which is the jar file name
'   working directory (this doesn't work but I use it nevertheless)
'   runas command which invokes elevation
'   0 means do not show the window. Normally, you show the window, but not this console window
'     which just blinks and disappears anyway
UAC.ShellExecute "run-normally.bat", "SomeFile.jar, strFolder, "runas", 0 

因为工作目录参数不起作用,我在bat文件中有两行:

rem Used as a helper for the elevating VBS script. Runs the jar file
rem given as 1st argument as if the file was double clicked.

rem Make sure we're on our CURRENT directory
cd /d %~dp0
rem Run java, expecting the jar file to be given as the 1st argument
javaw -jar %1

出于美观原因,我对此解决方案不满意。我想显示JRE的UAC消息,而不是Windows命令行:

image description

答案 2 :(得分:0)

我不使用Java,但您没有指定完整路径。你不能指望事情可靠地发挥作用。此外,无论您启动什么,都需要右键单击菜单“以管理员身份运行”,因为VBS代码运行该菜单命令,就像您单击它一样。如果它不在那里你不能点击它。 EXE有它。因此,请指定Javaw和jar文件的正确路径。

一个问题是当前目录根据您使用的技术而有所不同。总是指定完整路径。

这是一个脚本,列出了对象可用的动词(不是我们不使用文件而是使用对象)。图形shell(explorer)是一个对象浏览器,不知道文件是什么,它是某种类型的对象。

---------------------------
Windows Script Host
---------------------------

  ShVerb

  Lists or runs an explorer verb (right click menu) on a file or folder

    ShVerb <filename> [verb]

  Used without a verb it lists the verbs available for the file or folder

  The program lists most verbs but only ones above the first separator
  of the menu work when used this way

  The Properties verb can be used. However the program has to keep running
  to hold the properties dialog open. It keeps running by displaying
  a message box.
---------------------------
OK   
---------------------------

脚本

HelpMsg = vbcrlf & "  ShVerb" & vbcrlf & vbcrlf & "  David Candy 2014" & vbcrlf & vbcrlf & "  Lists or runs an explorer verb (right click menu) on a file or folder" & vbcrlf  & vbcrlf & "    ShVerb <filename> [verb]" & vbcrlf & vbcrlf & "  Used without a verb it lists the verbs available for the file or folder" & vbcrlf & vbcrlf
HelpMsg = HelpMsg & "  The program lists most verbs but only ones above the first separator" & vbcrlf & "  of the menu work when used this way" & vbcrlf & vbcrlf 
HelpMsg = HelpMsg & "  The Properties verb can be used. However the program has to keep running" & vbcrlf & "  to hold the properties dialog open. It keeps running by displaying" & vbcrlf & "  a message box." 
Set objShell = CreateObject("Shell.Application")
Set Ag = WScript.Arguments 
set WshShell = WScript.CreateObject("WScript.Shell") 
Set fso = CreateObject("Scripting.FileSystemObject")

    If Ag.count = 0 then 
        wscript.echo "  ShVerb - No file specified"
        wscript.echo HelpMsg 
        wscript.quit
    Else If Ag.count = 1 then 
        If LCase(Replace(Ag(0),"-", "/")) = "/h" or Replace(Ag(0),"-", "/") = "/?" then 
            wscript.echo HelpMsg 
            wscript.quit
        End If
    ElseIf Ag.count > 2 then 
        wscript.echo vbcrlf & "  ShVerb - To many parameters" & vbcrlf & "  Use quotes around filenames and verbs containing spaces"  & vbcrlf
        wscript.echo HelpMsg 
        wscript.quit
    End If

    If fso.DriveExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetFileName(Ag(0)))
'       Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
        Set objFolderItem = objFolder.self
        msgbox ag(0)
    ElseIf fso.FolderExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    ElseIf fso.fileExists(Ag(0)) = True then
        Set objFolder = objShell.Namespace(fso.GetParentFolderName(Ag(0)))
        Set objFolderItem = objFolder.ParseName(fso.GetFileName(Ag(0)))
    Else
        wscript.echo "  ShVerb - " & Ag(0) & " not found"
        wscript.echo HelpMsg 
        wscript.quit
    End If

    Set objVerbs = objFolderItem.Verbs

    'If only one argument list verbs for that item

    If Ag.count = 1 then
        For Each cmd in objFolderItem.Verbs
            If len(cmd) <> 0 then CmdList = CmdList & vbcrlf & replace(cmd.name, "&", "") 
        Next
        wscript.echo mid(CmdList, 2)

    'If two arguments do verbs for that item

    ElseIf Ag.count = 2 then
        For Each cmd in objFolderItem.Verbs
            If lcase(replace(cmd, "&", "")) = LCase(Ag(1)) then 
                wscript.echo(Cmd.doit)
                Exit For
            End If
        Next
    'Properties is special cased. Script has to stay running for Properties dialog to show.
        If Lcase(Ag(1)) = "properties" then
            WSHShell.AppActivate(ObjFolderItem.Name & " Properties")
            msgbox "This message box has to stay open to keep the " & ObjFolderItem.Name & " Properties dialog open."
        End If  
    End If
End If