Not sure if JAVA_HOME is set - bash

I have installed Maven on Ubuntu 12.04. The following is the output when I do
$ mvn --version
Listening for transport dt_socket at address: 8787
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_38, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/jdk1.6.0_38/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-52-generic", arch: "amd64", family: "unix"
but when I do
$ gedit /etc/environment
the output is
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
also the .bash_rc does not have any entry for JAVA_HOME and there is no .bash_profile file.
My Question
If JAVA_HOME is not set how mvn --version is showing a value of it?

If JAVA_HOME is not set how mvn --version is showing a value of it?
On my system, the mvn program is a shell script, which performs various operations to try to guess a value for JAVA_HOME, if a value is not already set. In particular, it examines the location of the javac program.
However, according to the answer of a related question, what mvn reports as "Java home" is not the value of the JAVA_HOME environment variable: it is the value of the java.home system proeprty

Can be set in /etc/.bash_profile or other system rc files.

Related

What should maven M2_Home be?

I installed maven with sudo apt install maven
Then, from this repo ran mvn clean install.
I got the following error
[ERROR] org.eclipse.xtext.maven.plugin.XtextGeneratorIT Time elapsed: 4.246 s <<< FAILURE!
java.lang.AssertionError:
Maven home not found. Tried to call 'mvn'.
Consider to set the envVar 'M2_HOME' or System property 'maven.home'.
Current settings are: maven.home=null M2_HOME=null
What should my M2_HOME be??? I typed mvn --version to get a clue:
$ mvn --version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.7, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_IL, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-109-generic", arch: "amd64", family: "unix"
But I'm not sure what to do now. Any help is appreciated, thanks!
M2_HOME is a home of Maven. It should be the root directory of your maven installation/binaries. It is used by script mvn (correspondingly mvn.bat on Windows).

JAVA_HOME is not working in maven

java is installed at this path
$ which java
/usr/bin/java
mvn -version is giving this error
$ mvn -version
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 have tried some of the solutions that were available online, but those don't work for me. Some of those solutions suggested adding
$export JAVA_HOME = /usr/libexec/java_home
or
$export JAVA_HOME = $(/usr/libexec/java_home)
to below files
~/.bashrc
~/.bash_profile
~/.profile
Also when I try to execute one shell command, it shows me error like
Error: JAVA_HOME is not defined correctly.
CARBON cannot execute /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java
After struggling for almost a day, I found out that maven is not reading the $JAVA_HOME from either of
~/.bashrc
~/.bash_profile
~/.profile
but it reads $JAVA_HOME from ~/.mavenrc
So finally, when I added
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home
in ~/.mavenrc then got output
mvn -v
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre
Default locale: en_CA, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"
To fixed it, update the JAVA_HOME like following :
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
Run Maven again :
$ mvn -version
maven reads JAVA_HOME from ~/.mavenrc. Add path for JAVA_HOME to ~./mavenrc and source it.
$ vim ~/.mavenrc
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .mavenrc
On Ubuntu
I faced a similar problem. I configured $JAVA_HOME in /etc/environment
like JAVA_HOME=PATH_TO_JDK for example JAVA_HOME=/home/max/jdk1.8.0_144
Careful with
White space after path declaration JAVA_HOME=/home/max/jdk1.8.0_144[[_NO_WHITE_SPACE_AFTER_DECLARATION]]
Don't put any quotes, e.g. JAVA_HOME="/home/max/jdk1.8.0_144"
Don't put /bin, e.g. JAVA_HOME=/home/max/jdk1.8.0_144/bin <- This is wrong
Please check once there should be no ';' in JAVA_HOME value, if there is semicolon then it would show above error.
I had
JAVA_HOME
set but did not export it and maven was not picking up the correct JAVA_HOME.
Once I exported JAVA_HOME then it got resolved. Do not forget to export JAVA_HOME so that the variable is available to child processes (UNIX* like OS)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home

Terminal is not sourcing .bash_profile, but sourcing .profile

I created a .bash_profile and added the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
export M2_HOME=/Users/aastha/apache-maven-3.3.9
export PATH=$PATH:$M2_HOME/bin
When I check echo $PATH, I get the following
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
(i.e) maven path is not imported.
and also mvn -version shows: mvn:command not found
When I manually source using source ~/.bash_profile and then do echo $PATH, I am able to see correct mvn path that I have entered in PATH variable in .bash_profile.
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/aastha/apache-maven-3.3.9/bin
mvn -version shows:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /Users/aastha/apache-maven-3.3.9
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.5", arch: "x86_64", family: "mac"
My terminal does not source .bash_profile until I manually source it using
source ~/.bash_profile
Note: I created a .profile with the similar content and found out in this case mvn -version shows the maven is installed. And terminal is sourcing .profile automatically.
Why is my terminal not able to source .bash_profile automatically
Could somebody please help?
Thanks in advance :D

How to change maven home directory?

I have maven home directory as below,
[root#localhost configuration-simple]# su -c "vi /etc/profile.d/maven.sh"
[root#localhost configuration-simple]# mvn -version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 19:21:28+0530)
Maven home: /home/kartyk/NetBeansProjects/motown.io/motown-develop/apache-maven-3.0.5
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /usr/java/jdk1.7.0_51/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.19.8-100.fc20.x86_64", arch: "amd64", family: "unix"
[root#localhost configuration-simple]#
Now I want to change the Maven home directory to some other folder, how could I do this through command prompt?
I assume you are familiar with basics of your OS.
go to Maven download page and get the latest version
extract it in a directory of your choice (/opt or /usr/local or ...)
set and export the environment variable MAVEN_HOME in your .bashrc
extend and export the environment variable PATH by $MAVEN_HOME/bin

Maven not found in Mac OSX mavericks

After upgrading my Mac OSX 10.8 to 10.9, Maven not found in the /usr/share path but its installed in 10.8.
when I try this command:
$ maven -version
got this result
-bash: mvn: command not found
but it worked fine on 10.8? Any idea?
Maven is not installed any more by default on Mac OS X 10.9. You need to install it yourself, for example using Homebrew.
The command to install Maven using Homebrew is
brew install maven
if you don't want to install homebrew (or any other package manager) just for installing maven, you can grab the binary from their site:
http://maven.apache.org/download.cgi
extract the content to a folder (e.g. /Applications/apache-maven-3.1.1) with
$ tar -xvf apache-maven-3.1.1-bin.tar.gz
and finally adjust your ~/.bash_profile with any texteditor you like to include
export M2_HOME=/Applications/apache-maven-3.1.1
export PATH=$PATH:$M2_HOME/bin
restart the terminal and test it with
$ mvn -version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 17:22:22+0200)
Maven home: /Applications/apache-maven-3.1.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: de_DE, platform encoding: MacRoman
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
brew install maven
Please ensure that you've installed the latest Xcode and Command Line tools.
xcode-select --install
Download Maven from here.
Extract the tar.gz you just downloaded to the location you want (ex:/Users/admin/Maven).
Open the Terminal.
Type " cd " to go to your home folder.
Type "touch .bash_profile".
Type "open -e .bash_profile" to open .bash_profile in TextEdit.
Type the following in the TextEditor
alias mvn='/[Your file location]/apache-maven-x.x.x/bin/mvn'
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdkx.x.x_xx.jdk/Contents/Home/
(Make sure there are no speech marks or apostrophe's)
8. Make sure you fill the required data (ex your file location and version number).
Save your changes
Type ". .bash_profile" to reload .bash_profile and update any functions you add. (*make sure you separate the dots with a single space).
Type mvn -version
If successful you should see the following:
Apache Maven 3.1.1
Maven home: /Users/admin/Maven/apache-maven-3.1.1
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.1", arch: "x86_64", family: "mac"
This solution could seem very long, but it's not. I just included many examples so that everything was clear. It worked for me in Mavericks OS.
Note: I combined and edited some of the answers shown above, added some examples and format and posted the result, so the credit goes mostly to the creators of the original posts.
Download Maven from here.
Open the Terminal.
Extract the file you just downloaded to the location you want, either manually or by typing the following lines in the Terminal (fill the required data):
mv [Your file name] [Destination location]/
tar -xvf [Your file name]
For example, if our file is named "apache-maven-3.2.1-bin.tar" (Maven version 3.2.1) and we want to locate it in the "/Applications" directory, then we should type the following lines in Terminal:
mv apache-maven-3.2.1-bin.tar /Applications/
tar -xvf apache-maven-3.2.1-bin.tar
If you don't have any JDK (Java Development Kit) installed on your computer, install one.
Type "java -version" in Terminal. You should see something like this:
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
Remember your java version (in the example, 1.8.0).
Type "cd ~/" to go to your home folder.
Type "touch .bash_profile".
Type "open -e .bash_profile" to open .bash_profile in TextEdit.
Type the following in TextEdit (copy everything and replace the required data):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk[Your Java version].jdk/Contents/Home
export M2_HOME=[Your file location]/apache-maven-[Your Maven version]/
export PATH=$PATH:$M2_HOME/bin
alias mvn='$M2_HOME/bin/mvn'
For example, in our case we would replace "[Your Java version]" with "1.8.0" (value got in step 5), "[Your file location]" with "/Applications" (value used as "Destination Location" in step 3) and "[Your Maven version]" with "3.2.1" (Maven version observed in step 3), resulting in the following code:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
export M2_HOME=/Applications/apache-maven-3.2.1/
export PATH=$PATH:$M2_HOME/bin
alias mvn='$M2_HOME/bin/mvn'
Save your changes
Type "source .bash_profile" to reload .bash_profile and update any functions you add.
Type mvn -version. If successful you should see the following:
Apache Maven [Your Maven version] ([Some weird stuff. Don't worry about this])
Maven home: [Your file location]/apache-maven-[Your Maven version]
Java version: [You Java version], vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk[Your Java version].jdk/Contents/Home/jre
[Some other stuff which may vary depending on the configuration and the OS of the computer]
In our example, the result would be the following:
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)
Maven home: /Applications/apache-maven-3.2.1
Java version: 1.8.0, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0</b>.jdk/Contents/Home/jre
Default locale: es_ES, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"
For me trying above techniques did work so I opened .bash_profile file and added following line in new line to connect to maven using short cmd :
alias mvn=/opt/apache-maven-3.6.3/bin/mvn
Restart your terminal and hit mvn clean install cmd
I am not allowed to comment to pkyeck's response which did not work for a few people including me, so I am adding a separate comment in continuation to his response:
Basically try to add the variable which he has mentioned in the .profile file if the .bash_profile did not work. It is located in your home directory and then restart the terminal. that got it working for me.
The obvious blocker would be that you do not have an access to edit the .profile file, for which use the "touch" to check the access and the "sudo" command to get the access
touch .profile
vi .profile
Here are the variable pkyeck suggests that we added as a solution which worked with editing .profile for me:
export M2_HOME=/apache-maven-3.3.3
export PATH=$PATH:$M2_HOME/bin
For me I had a AdoptOpenJDK 8 installed, instead of SE JDK 8. For which it was not able to recognize JAVA_HOME or mvn commands.
Check your java_home
/usr/libexec/java_home -V
and use JAVA SE SDK if it is different.
Then follow the above steps to install maven and check again

Resources