传递参数形成java程序调用另一个Java programa的论据bash脚本 [英] Passing arguments form java program to bash script that call another java programa with the arguments

查看:108
本文介绍了传递参数形成java程序调用另一个Java programa的论据bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我传递一个参数java程序执行shell脚本显示出波纹管:

I want to executing a shell scripting in my java program passing a argument showed bellow:

Runtime.getRuntime().exec("./test.sh " + "\\\"param1\\\"\\\"param2\\\"\\\"param3\\\"");

而test.sh将调用另一个Java程序传递字符串参数是这样的:

And the test.sh will call another java program passing the string argument like this:

another.jar \"param1\"\"param2\"\"param3\"

和最终方案anther.jar会间preT参数以这种格式

and finally the program anther.jar will interpret the argument in this format

another.jar "param1""param2""param3"

我这个bacause我不能正确地转义字符在这个situation..kkk处理有点迷惑

I'm a bit confuse with this bacause I can't deal correctly with escapes characters in this situation..kkk

我试图在第一个命令某些字符串格式,但我没有得到正确的形式。

I tried some strings formats in the first command but I didn't get the correct form.

一些帮助会没事!

THX!

推荐答案

我想你会过得更好使用<一个href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%5B%5D%29\"相对=nofollow> EXEC(的String [] cmdarray) 代替<一个href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29\"相对=nofollow> 执行exec(字符串CMD) 。这是因为<一href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29\"相对=nofollow> 执行exec(字符串CMD) tokenizes通过的 的StringTokenizer ,分手时在命令行参数而不理在所有双引号。

I think you would be better off using exec(String[] cmdarray) instead of exec(String cmd). This is because exec(String cmd) tokenizes the arguments via StringTokenizer, which pays no attention at all to double quotes when breaking up the command line arguments.

尝试是这样的:

ArrayList<String> argList = new ArrayList<String>();
argList.add("param1");
argList.add("param2");
argList.add("param2");
String[] args = argList.toArray(new String[argList.size()]);
Runtime.getRuntime().exec("mycommand", args);

在参数值内的字符不应该需要引用或逃逸,除非只要Java源代码code字符串可能需要逃跑。

Characters inside the param values should not need quoting or escaping, except insofar as Java source code string literals may require escaping.

这篇关于传递参数形成java程序调用另一个Java programa的论据bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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