如何使用Java编程从特定文件夹运行.bat或.vbs脚本 [英] How to run a .bat or .vbs script from a particular folder using java programming

查看:123
本文介绍了如何使用Java编程从特定文件夹运行.bat或.vbs脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,我需要从特定文件夹执行run.vbs脚本,例如C:\ Users \ Application \ Tools \ run.vbs.我只需指定位置即可从"Windows运行"启动脚本."C:\ Users \ Application \ Tools \ run.vbs",然后单击"Enter".但是,当我使用以下命令调用相同的脚本时:

  Runtime.getRuntime().exec("C:\\ Users \\ Application \\ Tools \\ run.vbs") 

出现错误无法访问指定的文件xxxx.war",这是vbs脚本正在访问的文件.

解决方案

您可以/必须加载CMD命令来执行BAT文件.语法是

  cmd.exe/c<命令名称>. 

此命令的Java为:

  Runtime.getRuntime().exec("cmd.exe/c<命令名称>")); 

供参考: http://ss64.com/nt/cmd.html

对于运行.VBS,可能是这样的:

  cscript< your_script> .vbs 

  wscript< your_script> .vbs 

Java命令:

  Runtime.getRuntime().exec("cscript< your_script> .vbs"); 

这里记录了它: https://technet.microsoft.com/en-us/library/ee156587.aspx

(我的回答取决于Windows.)

I am new to java, I need to execute the run.vbs script from a particular folder say C:\Users\Application\Tools\run.vbs. I am able to start the script from "windows run" by just giving the location i.e. "C:\Users\Application\Tools\run.vbs" and clicking on "Enter". But, when i call the same scirpt using:

 Runtime.getRuntime().exec("C:\\Users\\Application\\Tools\\run.vbs")

It is giving the error "unable to access the specified file xxxx.war" which is the file vbs script is accessing.

解决方案

You could/have to load the CMD command for executing BAT files. The syntax is

cmd.exe /c <command name>.

The Java for this command will be:

Runtime.getRuntime().exec("cmd.exe /c <command name>");

For referencing: http://ss64.com/nt/cmd.html

For running .VBS, this may be the way:

cscript <your_script>.vbs

or

wscript <your_script>.vbs 

Java command:

Runtime.getRuntime().exec("cscript <your_script>.vbs");

It is documented here: https://technet.microsoft.com/en-us/library/ee156587.aspx

(My answer is Windows dependent.)

这篇关于如何使用Java编程从特定文件夹运行.bat或.vbs脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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