How to run bootstrap.bat file from java program?

Answered

Sorry if this question is too general, I would just really like some guidance.

I'm trying to start an application called NiFi purely through java. My problem is running the bootstrap.jar file.
I've tested running a jar file I created and that works fine but when I swap that for the nifi bootstrap it doesnt launch nifi.

Its normally run with a ``run-nifi.bat file, so I've set all the environment variables in my java code and system properties. I've checked this by printing system properties and they are all as they should be. Running the bat file works fine but as I said I don't want to do this. Here is the bat file contents.

 :startNifi
pushd "%NIFI_ROOT%"
set LIB_DIR=lib\bootstrap
set CONF_DIR=conf

set BOOTSTRAP_CONF_FILE=%CONF_DIR%\bootstrap.conf
set JAVA_ARGS=-Dorg.apache.nifi.bootstrap.config.log.dir=%NIFI_LOG_DIR% - 
Dorg.apache.nifi.bootstrap.config.pid.dir=%NIFI_PID_DIR% - 
Dorg.apache.nifi.bootstrap.config.file=%BOOTSTRAP_CONF_FILE%

SET JAVA_PARAMS=-cp %CONF_DIR%;%LIB_DIR%\* -Xms12m -Xmx24m %JAVA_ARGS% 
org.apache.nifi.bootstrap.RunNiFi
set BOOTSTRAP_ACTION=run

cmd.exe /C "%JAVA_EXE%" %JAVA_PARAMS% %BOOTSTRAP_ACTION%

popd

%NIFI_LOG_DIR% and PID_DIR are both taken from an env.bat file and they just consist oif locations which I have put into my code.

Here is my code:

public class StartListener extends ClassLoader implements ActionListener {
private final String LIB_DIR = "lib\\bootstrap";
private final String CONF_DIR = "conf";

private final String BOOTSTRAP_CONF_FILE = CONF_DIR + "\\bootstrap.conf";
private final String BOOTSTRAP_ACTION = "run";
private final String NIFI_HOME = System.getProperty("user.dir") + "\\nifi-1.8.0";
private final String NIFI_LOG_DIR = NIFI_HOME + "\\logs";
private final String NIFI_PID_DIR = NIFI_HOME + "\\run";

public void setProperties() throws Exception {
    Properties systemProperties = new Properties(System.getProperties());
    FileInputStream runNifiProperties = new FileInputStream("systemProperties.txt");

    populateSystemProperties();
    systemProperties.load(runNifiProperties);
    System.setProperties(systemProperties);
}

public void populateSystemProperties() {
    try (FileWriter writer = new FileWriter(new File(System.getProperty("user.dir") + "\\systemProperties.txt"))) {
        writer.write("org.apache.nifi.bootstrap.config.log.dir = " + NIFI_LOG_DIR);
        writer.write(System.lineSeparator());
        writer.write("org.apache.nifi.bootstrap.config.pid.dir = " + NIFI_PID_DIR);
        writer.write(System.lineSeparator());
        writer.write("org.apache.nifi.bootstrap.config.file = " + BOOTSTRAP_CONF_FILE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void actionPerformed(ActionEvent e) {
        try {
            setProperties();
        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(-1);
        }
        String pathToJar = System.getProperty("user.dir") + "\\nifi-1.8.0\\lib\\bootstrap\\";
        try {
            System.out.println(System.getProperties());
            ProcessBuilder builder = new ProcessBuilder("java", "-classpath" + CONF_DIR + ";" + LIB_DIR, "-jar", System.getProperty("user.dir") + "\\nifi-bootstrap-1.8.0.jar");
            Process process = builder.start();
            process.waitFor();
            try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))){
                String line;
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (Exception excep) {
            excep.printStackTrace();
        }


}
}

I expect this to launch nifi which can be checked by trying to load it in browser, but what actually happens is nothing.

Any help or direction would be greatly appreciated.

0
1 comment

https://stackoverflow.com/ would be the more appropriate resource for this question since it's not IDE specific.

0

Please sign in to leave a comment.