Spring 引导 - 如何在 Process Builder 中指定 Java 路径

Spring boot - How to Specify Java path in Process Builder

我的系统中有 Java 7 作为默认配置。但我必须使用 Java 8 仅用于 spring 引导。

以下命令在我的终端中运行良好。

ubuntu@karthick:~$ JAVA_HOME=/opt/jdk1.8.0_181 mvn spring-boot:run

但是当我尝试从 Process Builder 执行它时它显示错误。

代码:

ProcessBuilder pb = new ProcessBuilder("JAVA_HOME=/opt/jdk1.8.0_181", "mvn", "spring-boot:run");
pb.directory(new File("/home/ubuntu/project/springbootexample"));
Process p = pb.start();

我遇到以下错误

error=2, No such file or directory. Stacktrace follows:
java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
    ...
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198)
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
    at net.bull.javamelody.JspWrapper.invoke(JspWrapper.java:150)
    at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:281)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
    at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:180)
    at com.brandseye.cors.CorsFilter.doFilter(CorsFilter.java:82)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

如何解决这个问题?

我建议创建一个脚本文件(下面的 test.sh),您可以在其中设置 java 主页并将其他命令添加到 运行 spring 引导并仅调用来自 java 的脚本使用以下代码:

  String[] args = { "/bin/bash", "test.sh"};

        StringBuilder builder = new StringBuilder();
        Process p2 = null;
        String result = null;
        String[] cmd = { "sudo", "-s" };
        Runtime.getRuntime().exec(cmd);
        p2 = Runtime.getRuntime().exec(args);
        BufferedReader reader = new BufferedReader(new 
       InputStreamReader(p2.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        result = builder.toString();