I have installed wsl2,and then installed jdk 17. Now I want to configure JAVA_HOME.
When I do:
$ which java
/usr/bin/java
$ whereis java
java: /usr/bin/java
But I don't see any java folder in /usr/bin.
Instead, I found java in /usr/lib/jvm:
$ pwd
/usr/lib/jvm
$ ls
java-1.17.0-openjdk-amd64 java-17-openjdk-amd64
Why so?
Because java isn't a folder, it's a binary, and probably a link to one of the binaries in jvm directory. You can check where the Java application points to by using ls -al /usr/bin/java
JAVA_HOME shouldn't point to the Java binary, but to one of the directories you've listed in the second example, so something like JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64.
I'd recommend having a look at jenv btw, it helps a lot in switching JDKs should you need it.
Windows 7 64-bit, with 64-bit JDK. Cordova plugman 1.4.2-dev. Using cygwin bash as a shell, but I don't see why that would affect anything.
$ echo $JAVA_HOME
"c:\Program Files\Java\jdk1.8.0_25"
$ echo $ANDROID_HOME
C:\Users\admin\AppData\Local\Android\Sdk
$ ls "c:\Program Files\Java\jdk1.8.0_25"
bin include lib release
COPYRIGHT javafx-src.zip LICENSE THIRDPARTYLICENSEREADME.txt
db jre README.html THIRDPARTYLICENSEREADME-JAVAFX.txt
$ ./main.js install --platform android --project ../myproject/ --plugin phonegap-facebook-plugin-gtg --variable APP_ID="1155264567919069" --variable APP_NAME="Voice Test"
Fetching plugin "phonegap-facebook-plugin-gtg" via npm
Installing "phonegap-facebook-plugin" for android
Failed to install 'phonegap-facebook-plugin':CordovaError: Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: "c:\Program Files\Java\jdk1.8.0_25"
I've also tried using JAVA_HOME without the quotes. What's going on?
#cubrr gave the correct answer in the comments. The error message being produced by Cordova is extremely misleading: the problem isn't that JAVA_HOME was incorrect, but that javac wasn't on my path. This was made all the more confusing by the fact that java was on my path, so when I checked it gave correct results. This is because the Oracle installer adds a directory to your path (c:\ProgramData\Oracle\Java\javapath) that contains links to java.exe and related programs but not the relevant JDK programs.
1) set > system variables > path to
`;C:\Program Files\Java\jdk1.8.0_xxx\bin;`
2) create new JAVA_HOME path with the value
C:\Program Files\Java\jdk1.8.0_xxx
now check javac version by issuing command at CMD
javac -version
*** don't touch the oracle - "C:\ProgramData\Oracle\Java\javapath;" path
If you are running windows 10, you need to restart your computer after changing the path and other variables. Possibly also on earlier windows versions.
When trying to run gradle, I get the following error:
# gradle
ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/default-java
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
However, when I check the JAVA_HOME variable I get:
# echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle
My JAVA_HOME is defined in .bashrc and I have double checked that it is set as the source.
Running java -version also confirms that JAVA_HOME is set correctly and is on the PATH.
# java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
I have also checked that /usr/bin/java symlinks to /etc/alternatives/java which in turn correctly symlinks to /usr/lib/jvm/java-7-oracle/jre/bin/java
Additionally I've checked that there are no duplicate JAVA_HOME definitions in .bash_profile or /etc/profile.
So my question is how/why does Gradle find /usr/lib/jvm/default-java, and more importantly how do I point it to the correct directory?
Other programs which require the JDK work fine, so I think its a Gradle issue. I've also tried reinstalling Gradle which made no difference.
I'm running 64bit Xubuntu (Ubuntu 13.10 base)
Turns out that the particular Gradle binary I downloaded from the Ubuntu 13.10 repository itself tries to export JAVA_HOME. Thanks to Lucas for suggesting this.
/usr/bin/gradle line 70:
export JAVA_HOME=/usr/lib/jvm/default-java
Commenting this line out solves the problem, and Gradle finds the correct path to the Java binary.
If you just download the binary from their website it does not have this problem,
It's an issue with the Ubuntu repo version. There also seem to be some other issues with 13.10 version.
add a symbolic link
sudo ln -s /usr/lib/jvm/java-7-oracle /usr/lib/jvm/default-java
Solution is to make JAVA_HOME == dir above bin where javac lives as in
type javac
javac is /usr/bin/javac # now check if its just a symlink
ls -la /usr/bin/javac
/usr/bin/javac -> /etc/alternatives/javac # its a symlink so check again
ls -la /etc/alternatives/javac # now check if its just a symlink
/etc/alternatives/javac -> /usr/lib/jvm/java-8-openjdk-amd64/bin/javac
OK so finally found the bin above actual javac so do this
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
above can be simplified and generalized to
which javac >/dev/null 2>&1 || die "ERROR: no 'javac' command could be found in your PATH"
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac) )))
For me this error was due to the reason Gradle as installed as sudo and I was trying as default user to run Gradle.
Try:
sudo gradle -version
or
sudo gradle -v
In my Ubuntu, I have a headache for 2 days on this issue.
Step 1. Type on the terminal whereis java
then it will display something like this
java: /usr/bin/java /etc/java /usr/share/java /usr/lib/jvm/java-8-openjdk-amd64/bin/java /usr/share/man/man1/java.1.gz
Step 2. Take note of the path:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
exclude the bin/java
your JAVA_HOME = /usr/lib/jvm/java-8-openjdk-amd64
Did you export your JAVA_HOME? Without export, the setting will not be propagated to the commands started inside of that shell. Also, java -version does not use JAVA_HOME, rather it uses the first java found in your path. Make sure your .bashrc looks something like this:
JAVA_HOME=/path/to/java/home
export JAVA_HOME
Try installing latest version of gradle,
sudo add-apt-repository ppa:cwchien/gradle
sudo apt-get update
sudo apt-get install gradle
If we install from ubuntu repo, it will install the old version , (for me it was gradle 1.4). In older version, it sets java home from gradle as export JAVA_HOME=/usr/lib/jvm/default-java. Latest version don't have this issue.
I faced this issue when I run the following command on Ubuntu:
ionic build android
To solve this issue, I did the following steps:
ln -sf /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java /usr/lib/jvm/default-java
Add JAVA_HOME to /etc/environment:
vi /etc/environment
Add:
JAVA_HOME="/usr/lib/jvm/default-java"
After saving, read it:
source /etc/environment
Finally, you can run build command.
I had the same problem, but I didnt find export command in line 70 in gradle file for the latest version 2.13, but I understand a silly mistake there, that is following,
If you don't find line 70 with export command in gradle file in your gradle folder/bin/ , then check your ~/.bashrc, if you find export JAVA_HOME==/usr/lib/jvm/java-7-openjdk-amd64/bin/java, then remove /bin/java from this line, like JAVA_HOME==/usr/lib/jvm/java-7-openjdk-amd64, and it in path>>> instead of this export PATH=$PATH:$HOME/bin:JAVA_HOME/, it will be export PATH=$PATH:$HOME/bin:JAVA_HOME/bin/java. Then run source ~/.bashrc.
The reason is, if you check your gradle file, you will find in line 70 (if there's no export command) or in line 75,
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
That means /bin/java is already there, so it needs to be substracted from JAVA_HOME path.
That happened in my case.
I have tested this on Manjaro Linux. Should work on other Disto too.
You need to include whole java-jdk dir instead of just java/bin for java env var.
For example, instead of:
export JAVA_HOME=/opt/jdk-14.0.2/bin #change path according to your jdk location
PATH=$PATH:$JAVA_HOME
use this:
export JAVA_HOME=/opt/jdk-14.0.2/ #change path according to your jdk location
PATH=$PATH:$JAVA_HOME
then run the gradle command it will work.
You can also go to the bin folder inside your gradle installation folder and correct the JAVA_HOME parameter in gradle.bat file.
In my case, my JAVA_HOME was set to c:\Program files\java\bin
The JAVA_HOME in gradle.bat was set to %JAVA_HOME%\bin\java.exe.
I corrected the JAVA_HOME in gradle.bat and it worked.
Thank you!!!
Before running the command try entering:
export JAVA_HOME="path_to_java_home"
Where path_to_java_home is the folder where your bin/java is.
If java is properly installed you can find it's location, by using the command:
readlink -f $(which java)
Don't forget to remove bin/java from the end of the path while putting it into JAVA_HOME
For me an explicit set on the arguments section of the external tools configuration in Eclipse was the problem.
sudo ln -s /usr/lib/jvm/java-7-oracle/jre /usr/lib/jvm/default-java
Create a symbolic link to the default-java directory.
You can find your java directory by
readlink -f $(which java)
# outputs: /usr/lib/jvm/java-7-oracle/jre/bin/java
# Remove the last `/bin/java` and use it in above symbolic link command.
I had a problem with this too. It said wrong directory when it was correct. So I just created a local variable with the name of JAVA_HOME omitting the final /bin/java. It worked fine for me.
If your GRADLE_HOME and JAVA_HOME environment are set properly then check your JDK directory and make sure you have java.exe file under below path.
C:\Program Files (x86)\Java\jdk1.8.0_181\bin
As error mentioned in gradle.bat file
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
It is not able to locate your java installation. So find and set
java.exe
under %JAVA_HOME%/bin if everything is correct.
This works for me (my account got disabled by client and their admin has removed java.exe from my directory.)
[Windows] As already said, it looks like .bat -file tries to find java.exe from %JAVA_HOME%/bin/java.exe so it doesn't find it since bin is repeated twice in path.
Remov that extra /bin from gradle.bat.
In my dockercontainer (being minimal the problem of not finding java) was, that "which" was not installed. Comipling a project using gradlew used which in ./gradlew to find java
Installing which solved the problem.
Adding below lines in build.gradle solved my issue .
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
#ISSUE:GradleException: javaConfig.targetVersion is Java 8, but the environment variable JAVA8_HOME does not exist
I think this is how you fix it in IntelliJ. I've run into it a couple times as I choose "Launch in New Window" for a new project but I haven't written down the steps yet.
1.File -> Project Structure. Project Settings / Project. Make sure Project SDK and Project language level are correct.
2.Preferences. Build, Execution, Deployment / Build Tools / Gradle. Make sure Gradle JVM is correct.
You may have to restart IntelliJ after this.
NOW it SOLVED THE ISSUE
Task :prepareKotlinBuildScriptModel UP-TO-DATE
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.3.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 22s
I tried to mvn install and got this message:
Compilation failure
Unable to locate the Javac Compiler in:
/usr/lib/jvm/java-7-openjdk-amd64/jre/../lib/tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
Well, there is an open jdk, I also downloaded another one. I tried to point JAVA_HOME to both, now it is set:
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_03
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
I also tried to choose one of those open with sudo update-alternatives --config java but got the same error with different jdk versions in it.
How can I fix that? Thanks in advance.
it seems like your PATH is not picked up correctly... does the output of "echo $PATH" contain the directory where javac resides?
I would suggest following:
open terminal and do an:
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_03
export PATH=$PATH:$JAVA_HOME/bin
javac -version
which javac
if javac -version still does not work create a symlink in /usr/local/bin pointing to your javac binary:
cd /usr/local/bin
ln -s /usr/lib/jvm/jdk1.7.0_03/bin/javac javac
this should get you up an running...
an alternative is to try setting up java via your package management system (e.g. "apt-get install java" or sth. similar)
I faced similar error on an ubuntu machine while running a maven build from Jenkins. If output of 'javac -version' prompts below messages:
"The program 'javac' can be found in the following packages:
default-jdk
ecj
gcj-5-jdk
openjdk-8-jdk-headless
gcj-4.8-jdk
gcj-4.9-jdk
openjdk-9-jdk-headless
Try: apt install "
Then you can install "openjdk-8-jdk-headless" using:
apt install openjdk-8-jdk-headless
This solved my problem and the maven build went through ok.
Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.
On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))
On Mac you can run $(dirname $(readlink $(which javac)))/java_home
I'm not sure about windows but I imagine where javac would get you pretty close
Just another solution, this one's cross platform (uses java), and points you to the location of the jre.
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Outputs all of java's current settings, and finds the one called java.home.
For windows, you can go with findstr instead of grep.
java -XshowSettings:properties -version 2>&1 | findstr "java.home"
echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.
If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk
FYI, if you are on a Mac, currently JAVA_HOME is located at
/System/Library/Frameworks/JavaVM.framework/Home
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
In case you don't have it setted, you can add in your .bashrc file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.
If $JAVA_HOME is defined in your environment...
$ echo $JAVA_HOME
$ # I am not lucky...
You can guess it from the classes that are loaded.
$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]
This method ensures you find the correct jdk/jre used in case there are multiple installations.
Or using strace:
$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
On the Terminal, type:
echo "$JAVA_HOME"
If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.
Did you set your JAVA_HOME
Korn and bash shells:export JAVA_HOME=jdk-install-dir
Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
C shell:setenv JAVA_HOME jdk-install-dir
Here's an improvement, grabbing just the directory to stdout:
java -XshowSettings:properties -version 2>&1 \
| sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3 /usr/java/jdk-17.0.2/bin/java
Enter to keep the current selection[+], or type selection number:
from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2
http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment
If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.