简体   繁体   English

从Java程序调用Shell脚本并将参数从Java for循环传递给Shell脚本

[英]Calling shell script from java program and passing arguments to a shell script from java for loop

Does anyone know if it is possible to call a shell script from within Java program and also pass an argument to that shell script from the for loop of that java class? 有谁知道是否可以从Java程序中调用Shell脚本,并且还可以从该Java类的for循环中将参数传递给该Shell脚本? In my shell script I am setting MySQL system variables to different values to see if those values affect the performance of the database application. 在我的shell脚本中,我将MySQL系统变量设置为不同的值,以查看这些值是否影响数据库应用程序的性能。 I could have set those values through JDBC, but as I am working with MySQL, it is not possible to restart the database from JDBC, after each query execution. 我可以通过JDBC设置这些值,但是当我使用MySQL时,在每次执行查询后都无法从JDBC重新启动数据库。

Yes it is possible. 对的,这是可能的。 For something like this you would probably be better off just using a batch file or something to do it though. 对于类似这样的事情,您最好只使用批处理文件或类似的方法。

If you really do need to use java try: 如果确实需要使用Java,请尝试:

How to run Unix shell script from Java code? 如何从Java代码运行Unix Shell脚本?

Runtime.exec() is what you are looking for. 您正在寻找Runtime.exec()

Runtime.getRuntime().exec("theScript.sh param1 param2);

..modulo exception handling. ..modulo异常处理。

You may want to look into Apache Commons 您可能要研究Apache Commons

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM