从java代码运行shell脚本

57hvy0tb  于 2023-10-23  发布在  Shell
关注(0)|答案(4)|浏览(175)

我有一个运行bash文件的java应用程序,在bash文件中,我有一个运行另一个java应用程序的代码,它似乎不起作用。我来自c++,这是一个非常容易的任务,但我对java没有真正的经验。下面是我的代码:

  1. import java.io.IOException;
  2. import java.net.*;
  3. import java.util.Scanner;
  4. public class start {
  5. public void executeScript(){
  6. try{
  7. ProcessBuilder pb = new ProcessBuilder("/root/Desktop/chat/script.sh");
  8. Process p = pb.start();
  9. p.waitFor();
  10. System.out.println("Script executed..");
  11. }catch(Exception e){
  12. e.printStackTrace();
  13. }
  14. }
  15. public static void main(String[] args) {
  16. start st = new start();
  17. System.out.println("I'm main..");
  18. st.executeScript();
  19. }
  20. }

这是我的bash文件:

  1. #!/bin/bash
  2. echo "bash started"
  3. java Client ip_address
  4. echo "bash finished"

下面是这段代码的结果:
我是主..脚本执行..
我知道“Script executed..”不应该打印出来,因为我试图从bash文件运行的java文件是一个无限循环。

注:If i run the bash file separately in the terminal i get an infinite loop which is the intended result and this is why i know that my mistake is from this file.

我希望我说得很清楚,如果没有,请询问更多信息。谢谢

eufgjt7s

eufgjt7s1#

另一种方法是使用Runtime.getRuntime()。这样的事情

  1. public void executeScript() throws IOException, InterruptedException {
  2. Process p = Runtime.getRuntime().exec("sh /root/Desktop/chat/script.sh");
  3. p.waitFor();
  4. BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
  5. BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  6. String line = "";
  7. while ((line = reader.readLine()) != null) {
  8. System.out.println(line);
  9. }
  10. line = "";
  11. while ((line = errorReader.readLine()) != null) {
  12. System.out.println(line);
  13. }
  14. }
展开查看全部
dluptydi

dluptydi2#

通过以上测试,您无法保证它是否正在运行。因为很明显,你告诉你在第二个Java应用程序中运行一个无限循环。现在我的建议是在无限循环中放入一些System.out.println语句,并使用下面的java代码来执行shell脚本。在output.txt文件中,你可以看到shell脚本和java程序的输出,你将知道应用程序是否成功执行。在shell脚本中也放一些echo语句。

  1. String[] command ={"/root/Desktop/chat/script.sh", "command line param if any"};
  2. ProcessBuilder pb = new ProcessBuilder(command);
  3. pb.redirectOutput(new File("/tmp/output.txt"));
  4. String result;
  5. String overall="";
  6. try {
  7. Process p = pb.start();
  8. p.waitFor();
  9. BufferedReader br = new BufferedReader(
  10. new InputStreamReader(p.getInputStream()));
  11. while ((result = br.readLine()) != null){
  12. overall = overall + "\n" + result;
  13. }
  14. p.destroy();
  15. System.out.println(result);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
展开查看全部
p1iqtdky

p1iqtdky3#

你也可以使用我的小库jaxec

  1. import com.yegor256.Jaxec;
  2. String stdout = new Jaxec("script.sh")
  3. .withHome("/home/me") // run it in this directory
  4. .withRedirect(false) // don't redirect STDERR to STDOUT
  5. .exec();

Jaxec类不仅会执行shell命令,还会将其输出发送到Slf4j log,等待其完成,将stderr重定向到stdout,并确保在退出代码不等于零时引发异常。

cuxqih21

cuxqih214#

如果你像写Process p = pb.start();那样启动一个进程,它会从java进程中多创建一个进程(我们通常说fork)。
1.例如,java作为进程'A'运行
1.如果进程“A”启动另一进程“B”,
1.那么它们是同时运行的。
(in你的情况下,A是'JAVA'和B是'Shell')
所以是2这两个进程同时(并行)运行,因此您将同时获得其中两个结果。

相关问题