Java Runtime Exec for VBA script with arguments

风流意气都作罢 提交于 2019-12-07 23:20:30

问题


I am trying to use Runtime exec() to run a vba script with arguements. I am having trouble passing in the args. I think I need to use the String[] overloaded method for exec.

Currently this works:

String command = "cmd /c \"\\concat2.vbs\""

Process p = Runtime.getRuntime().exec(command);

But I want to run that with arguments and if I do this

String command = "cmd /c \"\\concat2.vbs\" " + arg1 + " " + arg2

where arg1 and arg2 are strings my program doesnt run (status = 1)


回答1:


I think I need to use the String[] overloaded method for exec

Exactly! Change your command to be a String array. The array must contain the command and its arguments:

String[] command = {"cmd","/c", "concat2.vbs", arg1, arg2};
Process p = Runtime.getRuntime().exec(command);

concat2.vbs should be on Window's execution path (same directory, or configured in the PATH environment variable)

Check out the documentation for the Runtime class.




回答2:


Something like:

String[] cmd = { "cmd", "/c", "concat2.vbs" "dog" "house" };
Process p = Runtime.getRuntime().exec(cmd);

Should produce 'doghouse'



来源:https://stackoverflow.com/questions/2859537/java-runtime-exec-for-vba-script-with-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!