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

How to start java program (from .jar) elevated only using VBScript

有一个 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 

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

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

好吧,我如何从 vbs 文件中 运行 jar?这两个文件共享同一个目录。 java 应用程序的工作目录必须是 that 目录。

编辑: 所以感谢@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 

我可以在 collection 中添加另一个错误:

documentation 声明调用中的第一个参数是要启动的文件,将实参留给第二个参数。所以应该是(不好意思,没测试过)

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

到目前为止,我使这项工作没有疯狂的弹出错误的唯一方法是:

' 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 命令行:

我不使用 Java 但您没有指定完整路径。你不能指望事情可靠地工作。此外,无论您开始什么,都需要在它的右键单击菜单上有 运行 作为管理员,因为 VBS 代码运行该菜单命令就像您单击它一样。如果它不存在,则无法单击它。 EXE 有它。因此,请指定 Javaw 和 jar 文件的正确路径。

一个问题是当前目录因您使用的技术而异。始终指定完整路径。

这是一个脚本,列出了对象可用的动词(不是我们不是在处理文件,而是在处理对象)。图形 shell(资源管理器)是一个对象浏览器,除了它是某种类型的对象之外,不知道文件是什么。

---------------------------
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