0
votes

question

How can I ensure my install4j installer always finds only its java?

  • Can I create a top level installer which installs JRE to tmp, sets env variables and then starts the actual installer?
  • Can I load a vm file during installation?

problem

Install4j finds java 1.7 during install which impacts custom code preventing successful installation. I see found java7 prior to file deployment - ok expected given the JRE hasn't yet been unpacked.

evidence I created a simple installer and see the following:

BEFORE PATH=/opt/tools/Java/jdk1.7.0_79/bin:... JAVA_HOME=/opt/tools/Java/jdk1.7.0_79 ... ENV [JAVA_HOME] /opt/tools/Java/jdk1.7.0_79 ENV [PATH] /opt/tools/Java/jdk1.7.0_79/bin:...

installer details

envTest.install4j

  • Optional customer install script reporting found java prior at execution start

echo BEFORE echo PATH=$PATH echo JAVA_HOME=$JAVA_HOME echo Version: java -version

  • Run script reporting env after installer deployed jre

`

import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

Map<String, String> envMap = System.getenv();
SortedMap<String, String> sortedEnvMap = new TreeMap<String, String>(envMap);
Set<String> keySet = sortedEnvMap.keySet();
for (String key : keySet) {
    String value = envMap.get(key);
    Util.logInfo(this,"ENV [" + key + "] " + value);
}
return true;
1

1 Answers

1
votes

Actually, this turned our to be a problem with my custom code. The custom code launches an install4j generated executable via java. When launched on command line with wrong java found first, the launcher uses only its own java. When launched from my extension it fails.

Solution - set java in my extension:

  private File getInstalledJREDir() {
    return new File(installationDir, "jre");
  }

  private String addJREToFrontOfPathVar() {
    File jreBinDir = new File(getInstalledJREDir(), "bin");
    String path = System.getenv().get("PATH");
    if (null == path) {
      path = jreBinDir.getAbsolutePath();
    } else {
      path = jreBinDir.getAbsolutePath() + File.pathSeparator + path;
    }
    return path;
  }

  /**
   * Start Laucnher and block until it starts or timeout reached
   * @throws AutoRunException
   */
  public  void run() throws AutoRunException, IOException, InterruptedException {
    notifier.setPhase("Starting Agent");

    // Set Directories
    File dataDir = new File(installationDir.getParentFile(), "data-agent");
    File agentLog = new File(logDir,"agent.log");

    if (! isWindows()) {
      File agent = new File(installationDir, "bin/launcherExecutable");

      CmdExecutor ce = new CmdExecutor(agent, agentLog);

      // Ensure our installed JRE found 1st - PLAT-38833
      ce.updateEnvironmentVariable("JAVA_HOME", getInstalledJREDir().getAbsolutePath());
      ce.updateEnvironmentVariable("PATH", addJREToFrontOfPathVar());


      ce.setWorkingDir(installationDir);
      ce.setArgLine(String.format("--datadir %s", dataDir.getAbsolutePath()));

      notifier.logInfo("Starting " + agent + " with " + ce.getArgLine());
      if (! ce.run(true) ) {
        throw new AutoRunException("Agent failed to start " + ce.getOutput());
      }