I have an export command in my .bashrc to add the path of the java.exe file to PATH. Right now, running echo $PATH gives me this at the end
/mnt/c/Program Files/Java/jdk-14.0.2/bin
This is exactly where the java.exe and javac.exe files are stored, but when I run something like
java -version I'm getting the Command 'java' not found error. What am I doing wrong here?
You have added the Windows version of the Java binaries to the Path. You use WSL to run Linux binaries. (WSL1 is a compatibility layer, WSL2 uses the Linux kernel).
You have two options:
Install Java in your WSL environment. For example on Ubuntu with following commands:
sudo apt update
sudo apt install openjdk-14-jdk
After installing Java it will be available in the search path.
You could also run the Windows version by calling java.exe (note the .exe) on the name. This way WSL would call the Windows version. (https://learn.microsoft.com/en-us/windows/wsl/interop#run-windows-tools-from-linux)
If you want to use the windows version of java for some reason you where almost there. Try this please :)
java.exe -version
Install java in wsl:
sudo apt-get -y install openjdk-14-jdk
check the version.
java –version
check the path to config JAVA_HOME.
sudo update-alternatives --config java
eg
/usr/lib/jvm/java-14-openjdk-amd64/bin/java.
edit environment file to add the path:
sudo nano /etc/environment
add declare and add the JAVA_HOME:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$JAVA_HOME/bin"
JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64
save and check the path.
source /etc/environment
echo $JAVA_HOME
Output
/usr/lib/jvm/java-14-openjdk-amd64
Related
I'm trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash.
What's at the end of my bashrc:
export JAVA_HOME="/mnt/d/Program Files/Java/jdk-11.0.1"
export PATH="/mnt/d/Program Files/Java/jdk-11.0.1/bin:$PATH"
CMD INPUT/OUTPUT:
C:\Users\jaall>javac --version
javac 11.0.1
BASH INPUT/OUTPUT:
myubuntu_name#DESKTOP-LUK3BII:~$ javac --version
Command 'javac' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless
I've been stuck on this for awhile and can't figure it out or find a working solution online. Thanks!
As Biswapriyo suggested, you should use WSLENV.
Open PowerShell. Then set JAVA_HOME to the path to your java installation.
In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"
You should see a message that says "SUCCESS: Specified value was saved".
Then run setx WSLENV "JAVA_HOME/p".
You should see the success message again.
Type 'env' into your WSL bash prompt.
You should see JAVA_HOME correctly set at this point.
Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the \bin folder.
TL;DR: In WSL, you must use javac.exe since it is a Windows binary. Simply typing javac will not work, even if the path is set up correctly. If that doesn't work, try adding ../bin to the end of your JAVA_HOME variable.
Using Windows Binaries & Environment Variables in WSL
There's a much easier way to make Windows and WSL utilize the same JavaSDK binary, you just need to set up a few things first. Best of all, if you have JavaSDK installed on Windows, you do not need to install Linux binaries.
Check WSL Permissions and Directory Link (Optional, but recommended)
In WSL, list symbolic links on PC:
ls -l /mnt
If any drive is owned by root, perform your WSL dev work in /mnt/c/Users/<UserName>
Personally, I create a development directory in Windows and add a symbolic link to the directory in WSL:
ln -s /mnt/d/dev/environment/ ~/dev
cd dev now brings you to your development directory.
Ensure Java for Windows works
Open PowerShell/cmd.exe from any directory and enter: java --version
You should get a list of JRE info:
openjdk 11.0.4 2019-07-16 LTS
OpenJDK Runtime Environment Corretto-11.0.4.11.1 (build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.4.11.1 (build 11.0.4+11-LTS, mixed mode)
Your version might be different, the important part is that the system knows where to find Java. If you get an error, ensure your Windows Environment variables are set correctly:
JAVA_HOME as an Environment Variable, and
JAVA_HOME/bin as a Path variable.
Setting Variable in WSL
The best place to put the next lines of code are in your .bashrc file, but if you have a ./bash_profile or /etc/profile structure set up, you can put it there.
# Shared environment variables
# Use 'java.exe <args>' to utilize Windows Java binaries from within WSL.
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10
While we're at it, let's add Maven too:
export MAVEN_HOME=/mnt/d/software/apache-maven-3.6.2
I have my WSL, Java, and all my other dev tools set up on my second HDD which is not a system drive, ensure that your location matches your JAVA_HOME path in Windows.
For instance, if in Windows, Java is located at: C:\Java\jdk8.0
The corresponding WSL mount point is: /mnt/c/Java/jdk8.0
Executing
Important: Use java.exe <args> in WSL instead of java <args>
Say you just wrote CompareTwoStrings.class and want to compile and run it using the Windows binaries. You can do it from a Windows shell or WSL.
Windows PowerShell/cmd:
javac GetStringLength.java
java GetStringLength
WSL:
javac.exe GetStringLength.java
java.exe GetStringLength
Using java <args> in WSL will result in a Command 'java' not found error. That is because running windows binaries from within WSL requires that the .exe extension is used in the command.
Simplicity
We don't want to install a second copy of Java specific to WSL and waste that precious disk space, so we're going to call the Windows binary from the WSL shell. This is a great benefit of WSL—WSL1 in particular—in that it can interact (almost) flawlessly with the Windows File System.
NOTE: In order to run a program, it must either be a Path variable, or be run from within it's containing folder.
Hopefully that works as easily for you as it did for me. Just remember to use the correct command depending on what OS binary you're running. This took me about 10 minutes to get set up, and has been a lifesaver for cross-compiling and general ease-of-use.
I originally had Maven working in Windows attempted to run Maven in WSL2 and tried all of the previous solutions, but would consistently get the following no matter what I set for JAVA_HOME and PATH:
$ mvn -v
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
The issue was I was trying to use a Windows version of the JDK in the WSL2 Linux kernel. To fix this I ended up having to install a Linux version of the JDK (version 11) in the WSL as follows:
$ sudo apt update
$ sudo apt install openjdk-11-jdk
$ sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.
Then take the path for your JDK and use it to create JAVA_HOME and update PATH by appending the following in .profile
export PATH="/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH"
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Now close and reopen WSL2 and when your try again:
$ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.16.3-microsoft-standard-wsl2", arch: "amd64", family: "unix"
Since I've never been able to share variables between the 2 systems easily, I created a simple bash function which can easily retrieve (and define, if asked to) any Windows Environment variable.
It also takes care of paths so they get converted from Win32 to Un*x-like.
I added this to /etc/bash.bashrc:
winenv()
{
if [ "$#" == "0" ] || [ "$1" == "--help" ]
then
echo $'\n'Usage:
echo $'\t'winenv [-d] WINDOWS_ENVIRONEMENT_VARIABLE_NAME
echo $'\t'-d: Defines environment variable in current shell
echo $'\t Note that paths will be translated into un*x-like\n'
return
fi
local IFS='$\n'
local PATH_TO_TRANSLATE=$1
[ "$1" == "-d" ] && PATH_TO_TRANSLATE=$2
local VAR=$(cmd.exe /c echo %${PATH_TO_TRANSLATE}% | tr -d '\r')
local NEW=$(wslpath -u "${VAR}" 2>/dev/null || echo ${VAR})
echo "${PATH_TO_TRANSLATE} = ${VAR} -> ${NEW}"
[ "$1" == "-d" ] && export "${PATH_TO_TRANSLATE}=${NEW}"
}
And all I have to do to display one is to call winenv PROGRAMFILES (for example)
Or if I expect to export it, I just have to add a -d argument before the variable name as in winenv -d WINDIR.
I downloaded JDK and have many softwares which use requires it. For eg: for eclipse I'll copy the jdk in java folder in eclipse root directory. As a result I have many copies of JDK all over my system. Also dont know how to configure it for Sublime Text. Is is possible to install java just like in windows. I mean that I just open terminal and type java and it executes instead of changing directory everytime.
You could set your path variable but you really shouldn't be doing it like that.
Since you don't specify which version of Linux I'll be explaining it for Ubuntu and Arch Linux.
You should install openjdk packages with your package manager like so:
Ubuntu:
sudo apt-get install openjdk-8-jdk
Arch linux:
sudo pacman -S jdk8-openjdk
You can then use the following commands to change between those versions:
Ubuntu
sudo update-alternatives --config java
Arch Linux
archlinux-java set java-8-openjdk
You can even launch specific apps in specific versions but this would be too much to cover for now.
Setting the path variable regardless
To change your $PATH you have to either edit ~/.profile (or ~/.bash_profile) for each user or global $PATH setting in /etc/profile.
Simply append another path to it like so:
/usr/local/bin:/usr/local/sbin:/custom/path
You can also do:
PATH = $PATH:/custom/path
Multiple installation is never a good idea. You can have windows like setup but in *nix OS you have to follow different steps.
The preferred location for JAVA_HOME or /etc/environment. So Open /etc/environment in any text editor and add the following line:
JAVA_HOME="/usr/lib/your jdk path" (Use your java path)
If needed run else skip to next step source /etc/environment
See if done right?
echo $JAVA_HOME
Manual setup
Yes, just set PATH and JAVA_HOME suitably. For example, see this guide. This should give something like the following:
~/.bash_profile (or /etc/profile)
...
export JAVA_HOME=PATH_TO_JDK_ROOT/bin/java
export PATH=$PATH:PATH_TO_JDK_ROOT/bin
Linux distribution setup
Depending on your needs, you may otherwise opt to install your linux distribution JDK (using apt-get, yum, ...), thus avoiding manual steps to set the env vars.
Example: Ubuntu 16.04
To install the default JDK:
sudo apt-get install default-jdk
To install the Oracle JDK:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
To select the active Java version when there is more than one installed:
sudo update-alternatives --config java
If you want the appended value to be local to the script, do not prepend the export. Otherwise follow the example below which uses a search ability. I have appended to the PATH statement and want to export it so the PATH is known globally. If I invoke a second terminal, the if statement prevent the PATH environment variable from being double appended.
if [ ! "/home/mylogon/bin" == $PATH ];
then
export PATH=$PATH:/home/mylogon/bin:/home/mylogon/.local/bin"
fi
I have installed java in my CentOS release 5.5 machine using the command yum install java. But I am unable to compile a class using javac.
Do I need to install any other package?
I have tried to locate the javac executable but i am unable to locate it.
/usr/bin/java is linked as follows:
/usr/bin/java -> /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
I have seen the following output by yum list installed |grep java:
java-1.6.0-openjdk.x86_64 1:1.6.0.0-1.16.b17.el5 installed
tzdata-java.x86_64 2011b-1.el5 installed
Worked for me with this command:
yum install java-devel
You installed the Java Runtime Environment (JRE) only, which does not provide javac. For javac, you have to install the OpenJDK Development Environment. You can install java-devel or java-11-devel, which both include javac.
By the way: you can find out which package provides javac with a yum search, e.g.
su -c 'yum provides javac'
on more recent releases of CentOS e.g. 6 the command changes to
su -c 'yum provides */javac'
Use the following sudo command:
sudo yum install java-1.6.0-openjdk-devel
I don't know exactly what yum install java will actually install. But to check for javac existence do:
> updatedb
> locate javac
preferably as root. If it's not there you've probably only installed the Java runtime (JRE) and not the Java Development Kit (JDK). You're best off getting this from the Oracle site: as the Linux repos may be slightly behind with latest versions and also they seem to only supply the open-jdk as opposed to the Oracle/Sun one, which I would prefer given the choice.
I use Fedora (currently 31)
Even with JDK's installed, I still need to specify JAVAC_HOME in the .bashrc, especially since I have 4 Java versions using sudo alternatives --configure java to switch between them.
To find java location of java selected in alternatives
readlink -f $(which java)
In my case:
/usr/java/jdk1.8.0_241-amd64/jre/bin/java
So I set following in .bashrc to:
export JAVA_HOME=/usr/java/jdk1.8.0_241-amd64/jre/bin/java
export JAVAC_HOME=/usr/java/jdk1.8.0_241-amd64/bin/javac
export PATH=$PATH:/usr/java/jdk1.8.0_241-amd64/jre/bin
export PATH=$PATH:/usr/java/jdk1.8.0_241-amd64/bin/
Now javac –version gives:
javac 1.8.0_241
This is useful for those who want to use Oracle's version. Just remember to change your .bashrc again if you make a change with java alternatives.
Is the javac executable in a directory that is part of your PATH?
I don't know the CentOS equivalent of the Windows path but if you cd to the java sdk directory and run ./javac does anything happen?
Install same version javac as your JRE
yum install java-devel
This worked for me:
sudo dnf install java-<version>-devel
You have installed the Java Runtime Environment(JRE) but it doesn't contain javac.
So on the terminal get access to the root user sudo -i and enter the password.
Type yum install java-devel, hence it will install packages of javac in fedora.
Linux Mint 19.3
I installed Java Oracle manually, like this:
$ sudo ln -s /usr/lib/jvm/java-1.8.0_211/bin/javac /usr/bin/javac
for java 8 use
sudo yum install java-1.8.0-openjdk-devel
Make sure you install JDK/JRE first.
follow these steps:
open terminal go to your root dictionary by typing
cd /
you will see Library folder
Now follow this path Library/Java/JVM/bin
Once you get into bin you can see the javac file
Now you need to get the path of this folder for that just write this command
pwd
get the path for your javac.
Trying to install Java (JDK 6) on my new Ubuntu system and getting some bizarro errors. This is my first time ever using any flavor of Linux and so I'm sure it's a user issue (permissions or otherwise).
I downloaded the BIN file directly off Oracle's site (Java SE 6u23 for 64-bit Linux). This defaulted to downloading to /home/myUserName/Downloads.
From there I moved the file to /opt/java, which was a directory I created, because (as a Linux novice) that made sense to be the directory where Java should go.
I then ran the following 2 commands, per instruction I found online for running BINs:
chmod +x jdk-6u23-linux-x64.bin
sudo ./jdk-6u23-linux-x64.bin
Now, in my /opt/java directory I see both the BIN file and the jdk1.6.0_23 directory that seems to be intact upon inspection.
But, when I open a new terminal and run java -version, I get:
The program 'java' can be found in the following packages:
- gcj-4.4-jre-headless
- gcj-4.5-jre-headless
- openjdk-6-jre-headless
Try: sudo apt-get install
What is going on here?!?
(1) Was I wrong to try and make /opt/java my Java directory?
(2) Did I run the wrong commands?
(3) Is Java 1.6.0_23 even installed on my machine?
(4) What are all those gcj-xxx-headless targets?!?!
Thanks for any input!
Was I wrong to try and make /opt/java my Java directory?
Not really. Many Java developers install multiple JDK installations and always use /opt/jdk1.6.0_23 or similar paths. The bin file you downloaded is not an installer, but merely an extractor. It does not install the java binaries into system folders like /bin.
I usually download the JDK and execute it from within my home folder and afterwards move it to /opt and performing an chown.
Did I run the wrong commands?
Not really. In case you wanted to install a separate JDK, you did it correctly. In case you wanted system integration, you would be better off to use the distribution-specific packages, such as the one installed via aptitude install sun-java6-jdk or alike.
The bin you downloaded is imho more flexible, since I can use it to install multiple verisons of Java on the same system. I know this is something you don't often do on Linux machines.
If you want to use the java binary on command line, you'd have to manually set up the PATH and JAVA_HOME environment variables. I think on Ubuntu that's /etc/environment or /etc/profile or something like that.
Is Java 1.6.0_23 even installed on my machine?
Not really. See above answers.
What are all those gcj-xxx-headless targets?!?
The GCJ is the Gnu Compiler for Java. Obviously, it includes a Java Development Kit and a Java Runtime Environment.
Why downloading a bin, when you can simply:
sudo apt-get install sun-java6-jdk
If there isn't any special reason why you'd want that specific version from the site, you should use apt-get because it will take care of all the stuff like PATH variable, etc.
Please follow below steps to install oracle java:
Download the latest Java SE SDK version.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Untar the Archive:
tar -xzvf jdk-8-linux-x64.tar.gz
mv jdk1.8.0 /opt
cd /opt/jdk1.8.0
This step registers the downloaded version of Java as an alternative, and switches it to be used as the default:
update-alternatives --install /usr/bin/java java /opt/jdk1.8.0/bin/java 1
update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0/bin/javac 1
update-alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /opt/jdk1.8.0/jre/lib/amd64/libnpjp2.so 1
update-alternatives --set java /opt/jdk1.8.0/bin/java
update-alternatives --set javac /opt/jdk1.8.0/bin/javac
update-alternatives --set mozilla-javaplugin.so /opt/jdk1.8.0/jre/lib/amd64/libnpjp2.so
Test
To check the version of Java you are now running
java -version
Output
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)
To check the browser plugin browse to http://www.java.com/ and click “Do I have Java?”
Ref: https://askubuntu.com/questions/437776/ubuntu-13-04-unable-to-install-jdk7
You simply have put the JDK binaries in a directory. Although by convention /opt/java or /opt/jdk is often used, these are not directories that are automatically recognized by the system.
You can however update your PATH environment variable to include the /opt/java/bin dir, or symlink (ln -s) /opt/java/bin/java in one of the directories on your system that are included in your path like /usr/bin/
The JDK you installed from Sun/Oracle is the original JDK. The "headless" JDK is the open source alternative.
When you run the JDK BIN file, it simply extracts the archive. When you entered the java -version command, it found the FOSS Java, not the Java you had extracted in /opt. As somebody else had mentioned, developers keep multiple versions of the JDK. If you wish to use the Oracle's Java, then you need link /usr/bin/java to /opt/jdk1.6.0_23/bin/java.
sudo ln -s /usr/bin/java /opt/jdk1.6.0_23/bin/java
For this to work, the existing java command should be first delinked from the "headless" JDK. (Do the following before the previous command.)
sudo mv /usr/bin/java /usr/bin/java_old
This assumes that there is a link or executable named java in /usr/bin. Use the which command to be sure.
which java
To add a new pathname to the existing PATH variable, you need to type this in Terminal:
PATH=`echo $path`:/your/new/path
export PATH
If you had lost your original PATH variable, you could restore by entering this:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
export PATH
Try:
rm -rf /usr/bin/javac
rm -rf /usr/bin/jar
ln -s /home/jdk1.6.0_13/bin/javac /usr/bin/javac
ln -s /home/jdk1.6.0_13/bin/jar /usr/bin/jar
This way, your linux can find java && javac in /usr/bin
I got a message "No Java virtual machine could be found from your PATH
environment variable. You must install a VM prior to
running this program." Does anyone know how set up it correctly? Thanks!
If you install Java the Ubuntu way, you won't have to add it to the PATH yourself.
First, install Java from the Ubuntu repository:
sudo apt-get install sun-java6-jre
There are several other packages: sun-java6-jdk for the JDK, sun-java6-plugin for the browser plug-in etc.
Use Ubuntu's alternatives mechanism to select Sun Java 6 as the default version of Java that you want to use:
sudo update-alternatives --config java
This will present you with a menu where you can choose which version of Java you want to use by default (you can have multiple Java versions installed at the same time on your system).
See: https://help.ubuntu.com/community/Java
As the error message says, this looks like you have not set your PATH or JAVA_HOME environment variable correctly.
can you execute java from the command line? Try:
$ java -version
or
$ dpkg -L sun-java6-jre
Provided your are using debian or some derivative of it, if the "dpkg ..." command gives you any useful output, you should set the JAVA_HOME to the location of the installation directory, for instance
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.10
export PATH=$PATH:$JAVA_HOME
and you should be able to get java running.
First make sure you have the path to your java binaries.
locate javac
This will return a list of all locations matching "java". Look for something like "/usr/lib/jvm/java-6-sun-1.6.0.11".
Open /etc/environment with a text editor and add the following. Make sure you set JAVA_HOME to the actual path of the java installation directory you just found:
export JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.11"
export PATH="$PATH:$JAVA_HOME/bin"
I think that you need to install this package
sudo apt-get install sun-java5-bin
also check this
> #!/bin/bash
> # Init Script for j2re
> #This goes in /etc/profile.d
>
> JAVA_HOME=/usr/java/j2reVERSion
> PATH=$JAVA_HOME/bin:$PATH
>
> export JAVA_HOME PATH