简体   繁体   中英

How to set JAVA_HOME when I use the method "Runtime.getRuntime().exec" in Intellij Idea?

I want to use "jdeps" command to analyze some jar files' dependencies. My OS is Windows10. The core code is

String command = String.format("jdeps \"%s\"", path.toAbsolutePath().toString());
String format = String.format("cmd.exe /c %s", command);
process = Runtime.getRuntime().exec(format);

There are 2 JDK version in my computer, JDK11 and JDK8. I want to use JDK11 and I set environment variable JAVA_HOME to JDK11's path. When I use cmd windows to execute the code, it works as I want, but If I run it in Intellij Idea, the JDK is changed to JDK8.

I'm sure that I have set the Project Settings--Project--Project SDK to JDK11. Project language level is SDK default(11-Local variable syntax for lambda parameters). Modules--Sources--Language level is "11-Local variable syntax for lambda parameters". The Java Compiler of Intellij Idea is also set to 11. There is nothing about JDK8 in my Intellij Idea.

But If I run my code in Intellij Idea, the JDK is still changed to JDK8. That makes me crazy. Is it a bug? What can I do to set JDK11?

By the way, I exec the "setx JAVA_HOME" command to set JAVA_HOME to JDK11 in my code, but it does not work.

None of that uses JAVA_HOME .

When you execute jdeps like that, unqualified, you'll use the version that is on the PATH .

Either change the PATH , or qualify the command.

Eg I don't have Java on the PATH , by default, so running jdeps unqualified fails, but I can run it if I qualify the command:

C:\>jdeps -version
'jdeps' is not recognized as an internal or external command,
operable program or batch file.

C:\>C:\prog\Java64\jdk1.8.0_181\bin\jdeps -version
1.8.0_181

C:\>C:\prog\Java64\jdk-13.0.2\bin\jdeps -version
13.0.2

Intellij Idea use the PATH in environment variables if I did not set anything. I run echo %PATH% in my code. The result is different with system environment variables. I had tried to restart my Intellij Idea, but it dose not work. Finally, I restart my computer and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM