How do I set Path Variables in Linux just like in windows? - java

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

Related

WSL Bash isn't finding java in PATH

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

Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

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.

Java is installed but `echo $JAVA_HOME` doesn't seem to produce any output

I needed Maven for a Java project, so I installed it directly in a fresh Ubuntu installation using sudo apt install maven. It automatically downloaded Java 1.8_091 and installed it too. I ran echo $JAVA_HOME in the terminal but it doesn't produce any output. How do I rectify this? Is there any problem with environment variables.
When you download jdk and wants to install in ubuntu, then you need to create an env variable JAVA_HOME in .bashrc file and add this variable to $PATH variable. so that when you echo $JAVA_HOME, you will get the path to your java installation
As u have installed it using sudo apt, it doesn't set JAVA_HOME variable rather installs the java inside /usr/bin folder, and also set alternatives in ubuntu so that ubuntu would be able to identify java
To set JAVA_HOME only in actual shell session, invoke command:
export JAVA_HOME=/usr/lib/jvm/java-<version>
To persist this environment variable edit vi ~/.bash_profile file, and append it to it
To create the variable $JAVA_HOME, you've just to execute these lines :
JAVA_HOME=/pathToJavaJDKorJRE/java
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
Note : If you don't know the path of your JDK or JRE (pathToJavaJDKorJRE), use this command to find them.
find / -name 'javac'

Can `apt-get install` be used for JDK?

I've been using a linux machine for less than two weeks, so I am extremely new to Linux.
I'd also like to install the Java Developers Kit. How should this be done? Is there an apt-get command, or should I just click on the download button at Sun's website?
I'm sure there is more than one way to do this, and it probably doesn't matter much, but I was impressed with how smoothly apt-get install worked when installing PHP and MySQL, so if there is a right way to do this, I'd like to know.
I'm using Ubuntu version 9.04
Yes, definitely it's the suggested way to install JDK on your Linux system (if available).
On Ubuntu:
sudo apt-get install sun-java6-jdk
The Oracle JDK is no longer included in the Ubuntu repos. According to https://stackoverflow.com/a/15543636/192221 , the following ppa can be used:
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main" | tee -a /etc/apt/sources.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
apt-get update
apt-get install oracle-java7-installer
If you have OpenJDK installed already, you might need to use
update-alternatives --config java
to use the Oracle Java by default.
If you for some reason need to install the sun packages you can use the make-jpkg command to create a deb file and still have the files managed by apt/dpkg.
To do this:
sudo apt-get install java-package
fakeroot make-jpkg jre-1_5_0_06-linux-i586.bin
you will then have a .dpg file that you can install using for example gdebi
gdebi-gtk jre.deb
Also note that you can have multiple JDKs installed and switch between them using update-alternatives:
update-alternatives --config java
update-alternatives --config javac
and so on for other java related binaries, have a look in /etc/alternatives to find out which are available.
In Ubuntu 9.10 Karmic Koala apt-get and the Synaptic Software Manager both install version 6.15.1 of Sun Java and the JDK, which are now out of date, and an old version of Netbeans,, so don't use "sudo apt-get install" for these.
Go to the Sun Java download page
http://www.java.com/en/download/manual.jsp
Select the non-RPM 32-bit or 64-bit self extracting file, download it and check the file size of the downloaded file. Doesn't matter where you download it.
Click on the link to installation instructions for the corresponding file.
An easier way to set executable permission is to right click on the file in the file browser (Nautilus) and click Properties, then on the Permissions tab, click the Execute checkbox.
To test, open a new terminal and type:
java -version
You should see Java version "1.6.0_18" etc.
If you don't, you probably need to add some lines, adjusted to suit your installation directories, to .bashrc and .bash_profile which are both hidden files in your home directory.
gedit .bashrc
Add the following:
export JAVA_HOME=/opt/java/64/jre1.6.0_18
export PATH=$PATH:$JAVA_HOME/bin
Then edit your .bash_profile file and insert the same lines at the end:
gedit .bash_profile
Open a new terminal window and test as above.
If all went well, in the Applications menu > System Tools, you should now have the Sun Java Control Panel.
You can repeat a similar installation procedure for the JDK and Netbeans where you mark the downloaded .bin file as executable and then execute it from a terminal window in the directory you want to install into, such as /usr/local.
After installing the JDK, add to your .bashrc and .bash_profile files a line similar to this:
export PATH=$PATH:/usr/local/jdk1.6.0_18/bin
Netbeans 6.8 seems to be able to set itself up ok without any editing and adds itself to the Applications Programming menu.
My Ubuntu box has sun-java5-jdk and sun-java6-jdk available.
I am personally a linux mint person. However, I couldn't find this in my respitory:
sudo apt-get install sun-java6-jdk
After poking around for a bit, I was able to get this to work.
sudo apt-get install openjdk-6-jdk
Cheers.
I'm not sure Ubuntu 9.04. Is it openJDK?
But in Ubuntu 9.10 It is openJDK.
So It have many problem.
You can do like this to remove and install sun java jdk
sudo apt-get remove openJDK*
sudo apt-get install sun-java6*

Set Java environment variable problem on ubuntu

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

Categories