Discrepancy between OS terminal and IntelliJ terminal - java

I'm seeing some discrepancies between commands I execute on my plain old OS terminal (I'm using a Mac) and the terminal that ships with IntelliJ (the version I'm using is Ultimate 2019.2).
For one thing, Maven isn't recognized in IntelliJ, even though I've set the Maven home directory correctly in the IntelliJ settings to /opt/apache-maven-3.6.0 instead of the bundle that ships with IntelliJ. I've also ensured that the JDK in IntelliJ is set to /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/bin/java.
OS terminal:
$ which java
/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/bin/java
$ which mvn
/opt/apache-maven-3.6.0/bin/mvn
$ mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T13:41:47-05:00)
Maven home: /opt/apache-maven-3.6.0
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"
IntelliJ terminal:
$ which java
/usr/bin/java
$ mvn -v
bash: mvn: command not found
Can someone explain these discrepancies?

I solved this issue using a suggestion from this post (the third suggestion from #Kyle Strand). In the terminal settings in IntelliJ, I set the shell path for Bash to run in interactive mode using -i. I'm now using the same $PATH as my OS terminal and Maven command line is now recognized.

Related

Error "the JAVA_HOME environment variable is not defined correctly" on running "mvn clean javadoc:jar package"

When I try to execute the command
mvn clean javadoc:jar package
it shows
the JAVA_HOME environment variable is not defined correctly. This environment variable is needed to run this program.
NB: JAVA_HOME should point to a JDK, not a JRE.
I checked out the already asked question Unable to find javadoc command - maven and the solution I tried above were taken from this solution only. I am new to Ubuntu. How can I fix this?
Whereas when I run echo $JAVA_HOME it prints:
/usr/lib/jvm/java-11-openjdk-amd64
I also tried setting the JAVA_HOME to:
/etc/launchd.conf/java-11-openjdk-amd64
/usr/libexec/java-11-openjdk-amd64
/usr/libexec/java-11-openjdk-amd64/
When I run mvn -v, it prints:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "5.8.0-45-generic", arch: "amd64", family: "unix"
And when I run echo $JAVA_HOME it prints:
/usr/lib/jvm/java-11-openjdk-amd64
Hmm..., probably the file doesn’t exist for running javadoc. Does the file /usr/lib/jvm/java-11-openjdk-amd64/bin/javadoc exist?
Please try:
sudo apt install openjdk-11-jdk
I would remove the above listed packages with pattern openjdk-11-.* (see this question) and install Java like this.
On my Mac (big Sur) I was getting the same problem. After reading this article ; this command worked for my enviroment:
export JAVA_HOME=$(/usr/libexec/java_home)

Maven is using wrong java version

I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package I get the following error:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile
(default-compile) on project systembrett-backend: Fatal error compiling: invalid target release: 15 -> [Help 1]
So I suspect that maven is using the wrong Java version, because when i do mvn package -X for debug logs it start with:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
so it looks like, maven is using Java version 1.8.
But mvn -version says:
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: AdoptOpenJDK, runtime: /opt/jdk-15.0.2+7
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"
JAVA_HOME is /opt/jdk-15.0.2+7 and PATH is /opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin.
I thought maven is choosing the java version by checking JAVA_HOME, but apparently it is using an other version for the build. Does anyone know how to tell maven the correct version?
Thanks!
Following is a list of steps I use to troubleshoot this kind of issues:
Linux usually works with alternatives to ensure proper default Java environment is used. Similar to:
$ alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
+ 1 java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)
Try also for javac as it may not be configured the same way:
$ alternatives --config javac
For your maven instance, JAVA_HOME should be enough.
Your output of mvn -version testifies that you have configured it correctly. Remove your Java from your PATH to ensure JAVA_HOME and mvn will find the correct one.
The pom.xml can also configure the required compiler, similar to:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Another thing may be that you're not using the same ENV (different terminals may not have the same environment variables exported - I presume you being on CentOS, you already encountered this). You have to exit the terminal and get back in to allow the default variables to take effect.
You encounter this usually when different JRE vs JDK are used ( more: maven installation has runtime as JRE instead of JDK )
I had the same issue on RHEL 9.1. The solution was to look at /etc/java/maven.conf and set the correct JAVA_HOME here in this file.

Setting up Microsoft Code for java with WSL

I try to set the visual code settings for .csvode settings.json for wsl to the path where the wsl resides with the home folder. But I keep getting error that it does not point to a jdk. I have tried pointing to the local installation on windows as well as the place where the jdk resides on the wsl. None of which is working.
$ mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.8, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-18362-microsoft", arch: "amd64", family: "unix"
Tested and failed settings of the wsl settings:
{
"java.home": "C:\\Java\\java-11-openjdk-11.0.8.10"
}
and
{
"java.home": "/usr/lib/jvm/java-11-openjdk-amd64"
}
As well as variants where I add or remove slashes as well as bin folder.
Press Ctrl+Shift+P to open Command Platte and choose Java: Configure Java Runtime:
Turn to Java Tooling Runtime, JDK for Language Server will show all installed and eligible JDK for java extension, which is jdk11 or recent version. Choose the one that you've installed:
Then reload VS Code to make change effective.

Why does `mvn` show that `Java version: 9.0.1`, while my current `java` command is `1.8.0_151`?

I was wondering why mvn shows that Java version: 9.0.1, while my current java command is 1.8.0_151? Thanks.
$ mvn --version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /home/t/program_files/programming/Maven/apache-maven-3.5.2
Java version: 9.0.1, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-9-oracle
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.13.0-26-generic", arch: "amd64", family: "unix"
$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Note that I have installed both java 8 and java 9:
$ update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-9-oracle/bin/javac 1091 auto mode
* 1 /usr/lib/jvm/java-8-oracle/bin/javac 1081 manual mode
2 /usr/lib/jvm/java-9-oracle/bin/javac 1091 manual mode
$ update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-9-oracle/bin/java 1091 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode
2 /usr/lib/jvm/java-9-oracle/bin/java 1091 manual mode
and
$ which java
/usr/bin/java
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-8-oracle/jre/bin/java
and the only part in $PATH is
/usr/lib/jvm/java-8-oracle/bin:
/usr/lib/jvm/java-8-oracle/db/bin:
/usr/lib/jvm/java-8-oracle/jre/bin
and
$ echo $JAVA_HOME
/usr/lib/jvm/java-9-oracle/
A solution which I don't quite understand:
$ export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
$ mvn --version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /home/t/program_files/programming/Maven/apache-maven-3.5.2
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.13.0-26-generic", arch: "amd64", family: "unix"
Does mvn rely only on JAVA_HOME, while java doesn't?
The mvn command is actually a shell script, and that script makes the decisions about which version of Java is used for the JVM that runs maven.
The thing that determines which JVM is uses are the $JAVA_HOME and / or $JAVACMD environment variables:
Normally, $JAVA_HOME from your environment is used to find the java command. Typically `$JAVA_HOME/bin/java" is used.
If $JVMCMD is set, that overrides $JAVA_HOME
If you have an /etc/mavenrc file, or a ~/.mavenrc, then these are "sourced" first. So they may override the settings of $JAVA_HOME, etc from your environment.
If $JAVA_HOME or $JAVACMD are not set explicitly, then the "mvn" script tries to find the "best" Java installation by platform specific means. You may or may not end up with the version selected via "alternatives" and/or the current setting of $PATH.
Also note the you can set Java versions in your POM file. These affect the source and target versions for the code you build, but they do NOT affect the version of the JVM that runs your builds. (So if you were to select Java 9 in your POM and try build with a Maven on Java 8 JVM, then Maven would give an error.)
Does mvn rely only on $JAVA_HOME, while java doesn't?
Basically, yes with a couple of nuances:
mvn has fall-back logic for the case where $JAVA_HOME is not set.
java actually depends on how you have configured your $PATH. If you put $JAVA_HOME/bin (or similar) ahead of /usr/bin in your $PATH, then the shell won't see /usr/bin/java and your "alternatives" setting will be ineffective. (The alternatives mechanism for Java works by installing symlinks to commands as /usr/bin/java, /usr/bin/javac and so on.)
Does update-alternatives --config javac affect $JAVA_HOME?
It doesn't. The alternatives system works by updating symbolic links.
Bottom line:
If you want to ensure that mvn uses a specific Java version (without interfering with anything else), export the appropriate JAVA_HOME variable in your mavenrc file.
If you want to really understand what is going on with mvn, read the script!
Why does mvn show that Java version: 9.0.1, while my current java command is 1.8.0_151?
The short answer is because you have two different versions of Java installed. When you type java from the command line, it uses one version. mvn finds the other version.
You can see which java executable is used by typing
which java
The output from mvn shows that it is using the JDK installed at /usr/lib/jvm/java-9-oracle.
The command-line uses the PATH environment variable to search for executable files when you type a command. You can see the value of it by typing
echo $PATH
If both directories for Java 8 and Java 9 appear in the PATH, then the shell will use the first one found. You can tweak the order of PATH, but it is probably best just to remove the one that you don't want to use by default.
Does mvn rely only on JAVA_HOME, while java doesn't?
Yes, maven use $JAVA_HOMEenvironment variable to find java installation while terminal always refer $PATHto find executable files in the system.
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located
So if you need to change the java version which is referred by maven, you have to change the $JAVA_HOME variable.
Or else take a look at How to change maven Java version in pom.xml
does update-alternatives --config javac not affect JAVA_HOME?
No. See this

After Installing Java JDK 7 For Mac OS X - mvn -version still shows java version 1.6.0_31

Oracle released Java JDK 7 on April 26 for Mac OS X. I followed the install instructions and when I do java -version in a terminal window I get:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
However when I do mvn -version in the terminal window I get:
Apache Maven 3.0.2 (r1056850; 2011-01-08 18:58:10-0600)
Java version: 1.6.0_31, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.7.3", arch: "x86_64", family: "mac"
How can I get Maven to use the Java JDK 1.7.0_04?
Thanks for the help.
Finally found the answer here:
http://www.adam-bien.com/roller/abien/entry/java_se_development_kit_7
You should use JAVA_HOME=$(/usr/libexec/java_home) instead on a Mac and then set the current jdk via "Java Preferences.app".
Set JAVA_HOME in ~/.profile
The problem is that the symbolic link "CurrentJDK" inside the versions of JavaVm.framework points to the old jdk, so when i used the following commands to set the CurrentJDK to the latest one (1.7.0_45) it works
cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents CurrentJDK
reference: http://java.dzone.com/articles/installing-jdk-7-mac-os-x
To find the path from Java Preferences, try
/usr/libexec/java_home -X
My Java7 entry looks like this:
<dict>
<key>JVMArch</key>
<string>x86_64</string>
<key>JVMBundleID</key>
<string>com.oracle.java.7u04.jdk</string>
<key>JVMEnabled</key>
<true/>
<key>JVMHomePath</key>
<string>/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home</string>
<key>JVMIsBuiltIn</key>
<false/>
<key>JVMName</key>
<string>Java SE 7</string>
<key>JVMPlatformVersion</key>
<string>1.7</string>
<key>JVMVendor</key>
<string>Oracle Corporation</string>
<key>JVMVersion</key>
<string>1.7.0_04</string>
</dict>
You need to change the JAVA_HOME environment variable to the new JDK 1.7 location.
Look at this question for possible locations where JAVA_HOME might be defined. In a terminal, type which java to find the path of your Java installation, and then update JAVA_HOME to point to that path (but exclude the trailing bin folder).
In case anyone is installing Maven on a 64-bit Mac running Mac OSX 'Mavericks' save yourself some time and some hair pulling trying to get Maven installed. I was trying to follow this (which failed because the location of the java_home has changed on Mavericks:
http://maven.apache.org/download.cgi
I was pulling my hair out until I found this website, which recommends you Install homebrew and follow the instructions on this page: http://techspunky.blogspot.in/2013/10/how-to-install-maven-on-mac-osx-109.html
once complete don't panic when $ maven -version doesn't work, instead use $mvn --version as maven.apache.org/download.cgi recommended.
It worked, but then I noticed the Java SDK Maven was using was Java version: 1.6.0_65, which is not the latest SDK I installed on the system.
Once I set the JAVA_HOME=/usr/libexec/java_home
It was using the correct SDK:
Maven home: /usr/local/Cellar/maven/3.1.1/libexec
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
Success!
but wait...
next time I opened the terminal and ran mvn -v it went back to java version 1.6. WTH!
its some sort of supposed bug from what I read use:
echo JAVA_HOME=/usr/libexec/java_home -v 1.7 | sudo tee -a /etc/mavenrc
and now:
Maven home: /usr/local/Cellar/maven/3.1.1/libexec
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
The reason Maven is still using Java 6 is that the /usr/bin/mvn script that launches it does not use the correct OS/X method for resolving the current Java version as specified in Java Preferences. See this Maven issue for details:
http://jira.codehaus.org/browse/MNG-4226
Voting it up might get it fixed; in the meantime, if you alter the /usr/bin/mvn script as shown in the comments & patch on that issue you will get the desired result.
Ok on my machine. Yes, the Oracle installer didn't exactly do its job, and it confused me to no end after 1.6 was still running.
java run time was updated
java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
but the SDK HOME is actually a sim link still pointing to 1.6
/Library/Java/Home in finder GetInfo points to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
in Terminal
./Home -> /System/Library/Frameworks/JavaVM.framework/Home
Go figure.
I found 1.7 here and I'm using that to build with: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
I think the problem lies partly with the java launch mechanism. I have been able to get Eclipse to run my code under Java7, but I think eclipse itself is still running under Java6. Also, I have a java program that was last compiled a year ago, and built using jarbundler-2.1.0.jar (with option jvmversion="1.6+"). When I double click the .app file it runs Java6, but when I double click the .jar file inside the .app it runs Java7.
Also, I do not use JAVA_HOME, but I still get the correct java version "1.7.0_04" shown in the question. JAVA_HOME seems to be a red herring. Maybe Maven needs it, but Eclipse doesn't seem to use it, and neither does launching jar files.
And after setting Java7 in the Java Preferences tool, it does not seem to affect launching .app java files. However it does make 'java -version' from the terminal work nicely. I tried unchecking everything but the Java7 option, and then Eclipse wouldn't run at all.
If your JAVA_HOME is located at the local user's bash, mvn will pick the system's current java version no matter what JAVA_HOME you've set.
Hence first run the command 'which mvn' and check which executable mvn you are using. After that edit that mvn file and change the JAVA_HOME variable to your prefered java home.

Categories

Resources