如何从我的Java程序执行zookeeper-server-start .bat文件?

时间:2019-04-17 12:01:34

标签: java apache-kafka apache-zookeeper

我正在尝试从我的Java代码中启动zookeeper和kafka服务器。通常,我们通常手动执行批处理文件,我试图实现自动化,但是服务器永远无法启动。

我尝试使用相同的代码运行其他.bat文件,它们的运行就像一个超级按钮,但是zookeeper和kafka服务器的文件永远无法成功执行,也不会引发任何错误

<div class="main_wrapper">
  <h1>Interior Design</h1>
  <div id="showcase">
    <h2>Showcase</h2>
    <div class="category_wrapper">
      <div class="row">
        <div class="img-width col-md-6">
          <img src="https://via.placeholder.com/100" alt="">
        </div>
        <div class="img-width col-md-6">
          <img src="https://via.placeholder.com/100" alt="">
        </div>
      </div>
    </div>
  </div>
</div>

我希望Zookeeper服务器能够启动并保持启动状态,而实际上并不需要。请帮我哪里出问题了,这有可能吗?

2 个答案:

答案 0 :(得分:0)

下面的程序对我有用,它将所有日志打印到控制台,并等待该过程终止:

import java.io.*;
public class ExecuteProg {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c",
                    "E:/Softwares/kafka_2.11-2.0.0/bin/windows/zookeeper-server-start.bat",
                    "E:/Softwares/kafka_2.11-2.0.0/config/zookeeper.properties"});
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

我完全不知道为什么,但是添加“ start”标志使其对我有用。现在,我可以在代码中同时运行zookeeper和kafka服务器。我正在使用的代码如下,

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "start",
    "C:/kafka_2.11-2.1.0/kafka_2.11-2.1.0/bin/windows/zookeeper-server-start.bat",
    "C:/kafka_2.11-2.1.0/kafka_2.11-2.1.0/config/zookeeper.properties"});

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "start",
    "C:/kafka_2.11-2.1.0/kafka_2.11-2.1.0/bin/windows/kafka-server-start.bat",
    "C:/kafka_2.11-2.1.0/kafka_2.11-2.1.0/config/server.properties"});