如何仅使用VBScript启动Java程序(从.jar)提升权限 [英] How to start java program (from .jar) elevated only using VBScript

查看:407
本文介绍了如何仅使用VBScript启动Java程序(从.jar)提升权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个提供一个批处理文件的绝佳答案总是可以做什么,最好以升高的方式运行,如果没有,则不会升高已经升高了.

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.

我不想随程序一起分发批处理文件.答案的整个核心就是这个VBSScript:

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

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

非常简单.因此,我要使用jar文件的路径来代替批处理文件的路径.但这似乎不起作用:

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:

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应用程序的工作目录必须是那个目录.

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.

修改: 因此,谢谢@MCND(和),我现在知道参数如下:

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

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 

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

I can add another error in my collection:

推荐答案

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

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 

由于工作目录参数不起作用,所以在bat文件中有以下两行:

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

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

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

这篇关于如何仅使用VBScript启动Java程序(从.jar)提升权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆