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

时间:2021-02-28 20:48:02

There is a great answer providing a batch file what will allways do it's best to run elevated and will not elevate if already elevated.

提供一个批处理文件有一个很好的答案,它总是最好运行升级,如果已经提升则不会提升。

I don't want to distribute the batch file with my program though. The whole core of the answer is this VBSScript:

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

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

Pretty simple. So just instead of the path to the batch file, I want to use the path to a jar file. But it doesn't seem to work:

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

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

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

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

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

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

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

So well, how can I run the jar from the vbs file? Both files share the same directory. It's necessary that java application's working directory is that directory.

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

Edit: So thanks @MCND (and this) I now know that the arguments go as follows:

编辑:非常感谢@MCND(和这个)我现在知道参数如下:

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

And thanks to his code:

并感谢他的代码:

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

I can add another error in my collection:

我可以在我的集合中添加另一个错误:

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

3 个解决方案

#1


The documentation states that the first parameter in the call is the file to start, leaving the arguments to the second parameter. So it should be (sorry, not tested)

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

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

#2


So far, the only way I made this work without crazy popup errors is:

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

' 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 

Because the working directory parameter doesn't work, I have following two lines in the bat file:

因为工作目录参数不起作用,我在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

I am not satisfied with this solution for cosmetic reasons. I want to display the UAC message for JRE, not windows command line:

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

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

#3


I don't use Java but you aren't specifing full paths. You can't expect things to work reliably. Also whatever you are starting will need to have on it's right click menu Run As Administrator because the VBS code runs that menu command as if you clicked it. If it's not there you can't click it. EXE have it. So specify the correct paths to both Javaw and the jar file.

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

One issue is that the current directory is different depending on what technique you are using. ALWAYS specify full paths.

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

Here's a script that lists what verbs are available for an object (not we are not working with files but with objects). The graphical shell (explorer) is an object browser and has no idea what a file is apart from the fact it's an object of some type.

这是一个脚本,列出了对象可用的动词(不是我们不使用文件而是使用对象)。图形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   
---------------------------

The script

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

#1


The documentation states that the first parameter in the call is the file to start, leaving the arguments to the second parameter. So it should be (sorry, not tested)

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

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

#2


So far, the only way I made this work without crazy popup errors is:

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

' 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 

Because the working directory parameter doesn't work, I have following two lines in the bat file:

因为工作目录参数不起作用,我在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

I am not satisfied with this solution for cosmetic reasons. I want to display the UAC message for JRE, not windows command line:

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

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

#3


I don't use Java but you aren't specifing full paths. You can't expect things to work reliably. Also whatever you are starting will need to have on it's right click menu Run As Administrator because the VBS code runs that menu command as if you clicked it. If it's not there you can't click it. EXE have it. So specify the correct paths to both Javaw and the jar file.

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

One issue is that the current directory is different depending on what technique you are using. ALWAYS specify full paths.

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

Here's a script that lists what verbs are available for an object (not we are not working with files but with objects). The graphical shell (explorer) is an object browser and has no idea what a file is apart from the fact it's an object of some type.

这是一个脚本,列出了对象可用的动词(不是我们不使用文件而是使用对象)。图形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   
---------------------------

The script

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