'H2 Database: Error when doing migration of H2 from version 1 to 2 using Java ProcessBuilder

I am trying to migrate user data after executing alters to some tables in h2 from version 1 to version 2 using the Java process. When trying to ran command java -cp h2-1.4.192.jar org.h2.tools.RunScript -url jdbc:h2:./h2db -script migrate.sql through java ProcessBuilder, getting end result exit code as 1 which means failure. It is not giving me the reason for failure. While the same command is successful when I ran it through the terminal. I tried to set the JAVA_HOME in the process environment map then also got exit code 1. Sample code as below

try {
        ProcessBuilder pb = new ProcessBuilder("/usr/bin/java", "-cp", oldJarPath, "org.h2.tools.RunScript",  "-url" , "jdbc:h2:./h2db" , "-script" , "migrate.sql");
        Process process = pb.start();
        process.waitFor(4, TimeUnit.SECONDS);
        System.out.println("live:: "+process.isAlive());
        while (process.isAlive()) {
            process.waitFor(4, TimeUnit.SECONDS);
        }
        exitCode = process.exitValue();
        System.out.println("exit code:: "+exitCode);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

With Environment variables map

try {
        ProcessBuilder pb = new ProcessBuilder("java", "-cp", oldJarPath, "org.h2.tools.RunScript",  "-url" , "jdbc:h2:./h2db" , "-script" , "migrate.sql");
        Map<String, String> env = pb.environment();
        env.put("JAVA_HOME", "/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/");
        Process process = pb.start();
        process.waitFor(4, TimeUnit.SECONDS);
        System.out.println("live:: "+process.isAlive());
        while (process.isAlive()) {
            process.waitFor(4, TimeUnit.SECONDS);
        }
        exitCode = process.exitValue();
        System.out.println("exit code:: "+exitCode);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source