如何仅使用VBScript启动java程序(从.jar)

如何仅使用VBScript启动java程序(从.jar),vbscript,jar,uac,Vbscript,Jar,Uac,有一个问题是,在任何情况下,最好的做法是在高位运行,如果已经提升了,就不会提升 不过,我不想将批处理文件与我的程序一起分发。答案的核心是以下VBSSCcript: Set UAC = CreateObject("Shell.Application") UAC.ShellExecute "[path to the batch file which will run elevated]", "ELEV", "", "runas", 1 很简单。因此,我想使用jar文件的路径,而不是批处理文件的

有一个问题是,在任何情况下,最好的做法是在高位运行,如果已经提升了,就不会提升

不过,我不想将批处理文件与我的程序一起分发。答案的核心是以下VBSSCcript:

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 

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

编辑: 因此,感谢@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
多亏了他的密码:

Set UAC = CreateObject("Shell.Application") 
UAC.ShellExecute "javaw.exe", "-jar AutoClient.jar", "", "runas", 1 
我可以在我的集合中添加另一个错误:

声明调用中的第一个参数是要启动的文件,将参数留给第二个参数。所以应该是(对不起,没有测试)

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 
由于working directory参数不起作用,我在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代码会像您单击它一样运行该菜单命令。如果它不在那里,你就不能点击它。我有它。因此,指定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   
---------------------------
---------------------------
Windows脚本主机
---------------------------
ShVerb
列出或运行文件或文件夹上的资源管理器谓词(右键单击菜单)
动词[动词]
不带动词使用它列出可用于文件或文件夹的动词
该程序列出了大多数动词,但仅在第一个分隔符上方列出
使用这种方式时,菜单的所有部分都会起作用
可以使用Properties动词。然而,程序必须保持运行
保持“属性”对话框处于打开状态。它通过显示来保持运行
消息框。
---------------------------
好啊
---------------------------
剧本

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
HelpMsg=vbcrlf&“ShVerb”&vbcrlf&vbcrlf&“David Candy 2014”&vbcrlf&vbcrlf&“列出或运行文件或文件夹上的浏览器谓词(右键单击菜单)”&vbcrlf&vbcrlf&“ShVerb[动词]”&vbcrlf&vbcrlf&“不带动词使用它列出文件或文件夹可用的谓词”&vbcrlf&vbcrlf&vbcrlf
HelpMsg=HelpMsg&“程序列出了大多数动词,但只有在菜单的第一个分隔符“&vbcrlf&”上方的动词以这种方式使用时才起作用”&vbcrlf&vbcrlf
HelpMsg=HelpMsg&“可以使用属性动词。但是,程序必须保持运行”&vbcrlf&“以保持属性对话框打开。它通过显示“&vbcrlf&”消息框来保持运行。”
设置objShell=CreateObject(“Shell.Application”)
Set Ag=WScript.Arguments
设置WshShell=WScript.CreateObject(“WScript.Shell”)
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果Ag.count=0,则
wscript.echo“ShVerb-未指定文件”
wscript.echo HelpMsg
wscript.quit
否则,如果Ag.count=1,则
如果LCase(替换(Ag(0),“-”,“/”)=“/h”或替换(Ag(0),“-”,“/”=”?),则
wscript.echo HelpMsg
wscript.quit
如果结束
ElseIf Ag.count>2则
wscript.echo vbcrlf&“ShVerb-To many parameters”&vbcrlf&“在文件名和包含空格的谓词周围使用引号”&vbcrlf
wscript.echo HelpMsg
wscript.quit
如果结束
如果fso.DriveExists(Ag(0))=True,则
设置objFolder=objShell.Namespace(fso.GetFileName(Ag(0)))
'Set objFolderItem=objFolder.ParseName(fso.GetFileName(Ag(0)))
设置objFolderItem=objFolder.self
msgbox ag(0)
ElseIf fso.FolderExists(Ag(0))=True然后
设置objFolder=objShell.Namespace(fso.GetParentFolderName(Ag(0)))
设置objFolderItem=objFolder.ParseName(fso.GetFileName(Ag(0)))
ElseIf fso.fileExists(Ag(0))=True然后
设置objFolder=objShell.Namespace(fso.GetParentFolderName(Ag(0)))
设置objFolderItem=objFolder.ParseName(fso.GetFileName(Ag(0)))
其他的
wscript.echo“ShVerb-”&Ag(0)和“未找到”
wscript.echo HelpMsg
wscript.quit
如果结束
Set objVerbs=objFolderItem.Verbs
'如果只有一个参数列出该项的动词
如果Ag.count=1,则
对于objFolderItem.Verbs中的每个cmd
如果len(cmd)0,则CmdList=CmdList&vbcrlf&replace(cmd.name,&,“”)
下一个
wscript.echo mid(CmdList,2)
'如果两个参数不支持该项的动词
ElseIf Ag.count=2那么
对于objFolderItem.Verbs中的每个cmd
如果lcase(替换(cmd,&,“”)=lcase(Ag(1)),则
echo(Cmd.doit)
退出
如果结束
下一个
“财产是特殊情况。脚本必须保持运行,才能显示“属性”对话框。
如果Lcase(Ag(1))=属性,则
WSHShell.AppActivate(ObjFolderItem.Name和“属性”)
msgbox“此消息框必须保持打开状态才能使“&ObjFolderItem.Name&”属性对话框保持打开状态。”
如果结束
如果结束
如果结束

很抱歉,这一个抛出了e
---------------------------
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