Installing Oracle JDK on Windows subsystem for Linux
Asked Answered
W

13

92

When trying to use the Linux version of Oracle's JDK on the latest Windows 10 build having support for bash, I am running into a problem with the prompt hanging whenever attempting to invoke the java binary.

Typing even something as simple as java -version hangs and I have to terminate the process to resume control.

Anyone got this working yet?

Wanda answered 7/4, 2016 at 14:9 Comment(11)
I'm really not sure what you're trying to do. There is a Windows version of the JDK available - why are you using the Linux version? Can you tell us a bit more about what it is you're doing?Trunnel
I would also like to know as some of us in the office have successfully done the install on Windows 10 and some of us get the hang.Purnell
I didn't try Oracle JDK but I installed Ubuntu's own openjdk-jre-headless and it works fine. Maybe Oracle JDK depends on a head but bash is only CLI?Antione
You'll need to iinstall the Windows JDK/JRE. Installing Ubuntu's command shell won't make the Linux JDK/JRE run under Windows.Crossbar
@Trunnel the windows JDK is inaccesible inside bash shell. actually both the environments are isolated from each other as far as i read. so cross using their utilities are out of questionWanda
@Antione yeah openJDK is my next bet, but was hoping to run some hadoop binaries which work well with oracle JDKWanda
@Bill was actually hoping for that, isn't the windows version of bash shell just a wrapper providing a linux compatible kernel interface for all your native ubuntu utilities. then why should JDK be any differentWanda
@SaurabhMishra I tried again. Even OpenJDK does not fully work. You can run a bare javac command, but if you try to compile any file it hangs again.Antione
My attempts have ended poorly with apt-get problems trying to resolve /proc. The Oracle JRE extracts just fine, but won't run.Hubert
@Hubert check out the discussion at the github.com/Microsoft/BashOnWindows/issues/49. It seems the issue with the default-jdk has been resolved and you can try apt-get it. Also what's that problem you are mentioning about /proc?Wanda
@SaurabhMishra can you adjust your chosen answer as this is possible now.Clave
C
93

I wanted to clarify that as of 9 December 2016, you most certainly can install Java 8 on Ubuntu Bash for Windows 10 and that @Karl Horton is correct.

You will need to install unzip sudo apt-get install unzip

Copy this script somewhere in your bash for windows session and make it executable (chmod +x filename). If you do not use a command line based editor such as vim then you will have windows line endings to deal with. you can use dos2unix or your preferred way of dealing with that. I just paste it into a file using vim.

 #!/bin/bash

set -ex

# UPDATE THESE URLs
export JDK_URL=http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
export UNLIMITED_STRENGTH_URL=http://download.oracle.com/otn-pub/java/jce/8/jce_policy-8.zip

# Download Oracle Java 8 accepting the license
wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
${JDK_URL}
# Extract the archive
tar -xzvf jdk-*.tar.gz
# clean up the tar
rm -fr jdk-*.tar.gz
# mk the jvm dir
sudo mkdir -p /usr/lib/jvm
# move the server jre
sudo mv jdk1.8* /usr/lib/jvm/oracle_jdk8

# install unlimited strength policy
wget --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" \
${UNLIMITED_STRENGTH_URL}
unzip jce_policy-8.zip
mv UnlimitedJCEPolicyJDK8/local_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/
mv UnlimitedJCEPolicyJDK8/US_export_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle_jdk8/jre/bin/java 2000
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle_jdk8/bin/javac 2000

sudo echo "export J2SDKDIR=/usr/lib/jvm/oracle_jdk8
export J2REDIR=/usr/lib/jvm/oracle_jdk8/jre
export PATH=$PATH:/usr/lib/jvm/oracle_jdk8/bin:/usr/lib/jvm/oracle_jdk8/db/bin:/usr/lib/jvm/oracle_jdk8/jre/bin
export JAVA_HOME=/usr/lib/jvm/oracle_jdk8
export DERBY_HOME=/usr/lib/jvm/oracle_jdk8/db" | sudo tee -a /etc/profile.d/oraclejdk.sh

And now I can do the following

fieldju@DESKTOP-LTL6MIC:~$ java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)                                                

The links and versions in the above script are likely to be out of date by the time you read this, so just head over to http://www.oracle.com/technetwork/java/javase/downloads/index.html accept the license so that their js lets you copy the new URLs and you should be good to go.

Clave answered 10/12, 2016 at 4:35 Comment(6)
You should add that unzip is required for this script (since it's not installed by default)Dithyramb
Getting a weird host issue sudo: unable to resolve host DESKTOP-6PMR0AI on a friends computer.Ermelindaermengarde
Now Unlimited cryptography enabled by default in the JDKCash
Man, you are the God of automation. :)Alight
This script rocks. Today is 10/16/2020 and now I have jdk8 in my Windows/Ubuntu shell.Julesjuley
A small but important piece is missing. This script works, but if you already have java 11 or 15 installed it doesn't change the default version of java. To do that - run sudo update-alternatives --config javaPersas
S
69

It seems in 2017 august the solution is simpler as suggested by @noah-david.

I was able to install Oracle JDK 8 from the “WebUpd8” team repository.

Instructions. To add the repository:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

To install:

sudo apt-get install oracle-java8-installer 
sudo apt install oracle-java8-set-default

After install

costin@amanta-win:/mnt/c/work$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

costin@amanta-win:/mnt/c/work$ which java
/usr/bin/java

costin@amanta-win:/mnt/c/work$ uname -a
Linux amanta-win 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 
  x86_64 x86_64 x86_64 GNU/Linux
Screwy answered 10/8, 2017 at 8:34 Comment(6)
Agreed this is now the best approach. The oracle-java8-installer requires first adding the webupd8team ppa: sudo add-apt-repository ppa:webupd8team/javaDeweese
adding repository returns "Cannot add PPA: 'ppa:~webupd8team/ubuntu/java'. ERROR: '~webupd8team' user or team does not exist."Strepitous
@TimHolt got the same message when I misspelled the name of the team.Bothersome
Maybe the user got deleted because I've also have error that it don't exists.Seclusive
Thanks to Oracle, this will NOT work any longer as they changed the licensing starting April 16, 2019 - I would rather use Zulu or Corretto instead of using this PPA - refer to linuxuprising.com/2019/04/… for details on installing Zulu SDKNorfolk
I tried this method today to install java 14.0.2 on WSL and it worked. Article to follow along: linuxuprising.com/2020/03/…Solifluction
F
54

I must be missing something... all I did was:

sudo apt-get update
sudo apt-get install default-jdk

java -version

output:

java version "1.7.0_131"
OpenJDK Runtime Environment (IcedTea 2.6.9) (7u131-2.6.9-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)
Foxing answered 1/6, 2017 at 13:36 Comment(6)
This is how it should be! The question asks for Oracle JDK, though. You're installing Open JDK.Firer
If you want Oracle JDK, type sudo apt-get install oracle-java8-installerAuroreaurous
Also, this is not working in Windows Subsystem for Linux. Or Linux Subsystem for Windows, however it is called.Aquilar
I just ran sudo apt-get install default-jdk on Windows 10 18.04 with Ubuntu 18.04 and it installed OpenJDK 11.Tradeswoman
default-jdk nowadays is set to 11, but what if one needs to install jdk 8?Claudclauddetta
The questions asks about Oracle JDK, this answer fails to point out it does not use it rather installs open JDK and what the difference is. Please edit the answer to improve this as not might notice this.Razor
C
9

I used the script given by @fieldju but he missed some things that the script depends on, and also copy/pasting the contents results in having windows line endings/carriage returns (/r) which will need replacing to linux returns. Also, I found it a lot more straightforward to download the zips needed first and put them alongside the script. Here's a full list of what I did:

  1. In bash, type sudo apt-get install zip unzip to make sure unzip/zip is installed on your bash console
  2. Download the latest Linux version of the Java JDK from the oracle website (I have a 64 bit system so I chose "Linux x64") and save it in a folder somewhere on your computer that you can get to in bash NOTE: don't change the file name to ensure it works with the script
  3. Download the unlimited strength policy seperately in the same folder as the last zip, again ensuring you keep the filename as-is.
  4. Copy and paste the following script into notepad and save it as java_install_predownloaded.sh in the same folder alongside the zips:

Script:

#!/bin/bash

# Extract the archive
tar -xzvf jdk-*.tar.gz

# mk the jvm dir
sudo mkdir -p /usr/lib/jvm
# move the server jre
sudo mv jdk1.8* /usr/lib/jvm/oracle_jdk8

# install unlimited strength policy
mv UnlimitedJCEPolicyJDK8/local_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/
mv UnlimitedJCEPolicyJDK8/US_export_policy.jar /usr/lib/jvm/oracle_jdk8/jre/lib/security/

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/oracle_jdk8/jre/bin/java 2000
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/oracle_jdk8/bin/javac 2000

sudo echo "export J2SDKDIR=/usr/lib/jvm/oracle_jdk8
export J2REDIR=/usr/lib/jvm/oracle_jdk8/jre
export PATH=$PATH:/usr/lib/jvm/oracle_jdk8/bin:/usr/lib/jvm/oracle_jdk8/db/bin:/usr/lib/jvm/oracle_jdk8/jre/bin
export JAVA_HOME=/usr/lib/jvm/oracle_jdk8
export DERBY_HOME=/usr/lib/jvm/oracle_jdk8/db" | sudo tee -a /etc/profile.d/oraclejdk.sh

This code is a modified version from @fieldju which assumes the zips are already downloaded and in the same folder as this .sh file

  1. because the file has the windows carriage returns you need to ensure they are replaced, so in bash navigate to where you saved java_install_predownloaded.sh and run the following command:

    sed 's/^M$//' java_install_predownloaded.sh > java_install_predownloaded_unix.sh

I also then ran the following to ensure there are definitely no line endings from windows:

sed 's/\r$//' java_install_predownloaded_unix.sh > java_install_predownloaded_unix_final.sh

  1. After running those 2 lines, a file called java_install_predownloaded_unix_final.sh will be in the folder which is our 'cleaned' version without the windows line endings, so you just need to execute ./java_install_predownloaded_unix_final.sh in bash and watch the magic happen. Hey Presto you now have java installed on your bash instance on windows!
Cheltenham answered 8/2, 2017 at 10:21 Comment(1)
BTW, if you create the script using vim, or your command line based editor of choice, it does not add the windows line endings. However if your using notepad or some other windows based editor then yes to all of your stuff. I just re-installed windows and had to reefer to my own post for getting my dev env back. :)Clave
C
6

The steps I did for a fresh install of Oracle JDK 8 on my Bash (Windows 10):

  1. sudo apt-get install python-software-properties

  2. sudo add-apt-repository ppa:webupd8team/java

  3. sudo apt-get update

  4. sudo apt-get install oracle-java8-installer

  5. Accept licenses

    Image

You're done! Check your java version using java -version and expected output should be something like this:

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)

P.S. At the time of writing this, stable version Oracle 9 JDK has been released, you could probably tweak step 4 above.

Crosley answered 24/10, 2017 at 3:45 Comment(0)
W
5

Seems installing JDK8 is not working at the moment on Build#14316 of WSL. But trying to install JDK7 worked fine for me. Exploring the limits of this installation at the moment and will keep posted. Idea came from here : https://github.com/Microsoft/BashOnWindows/issues/196

Wanda answered 16/4, 2016 at 6:11 Comment(2)
My bet is that it has something to do with the new JavaFX UI stuff (in the default runtime in Java 8). Java 7 also supports JavaFX, but the jar is not loaded by default.Chincapin
As a status update to this for people facing the same issue, it seems to have been resolved by a Windows Update. I can no run oracle-jdk8 without issue.Unnamed
N
5

I'm confirming here that Oracle JDK Version 8u102, x64 for linux is installable ok on Windows 10 Insider Build 14905, released on 16th August 2016.

Downloadable from here after accepting license

C:\>systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows 10 Pro Insider Preview
OS Version:                10.0.14905 N/A Build 14905

I followed the instructions here: wikiHow Oracle Java Install

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

$ javac -version
javac 1.8.0_102

I've not yet had a chance to really stress this install, but this is better than the previous hangs.

Notional answered 22/8, 2016 at 1:52 Comment(1)
haven't tried your installation instructions but from installing it from the webupd8team/java ppa it doesn't work.Tamalatamale
M
5

A lot of answers are recommending installing the Linux JDK even though they have a perfectly good Windows JDK. The beauty of WSL is preventing you from having to do things like this, as the file systems are interoperable. As long as you take the file extensions into account (outlined below), you would only have to install both for niche situations.

Straightforward Method

You can do this very simply by adding the following line of code to your .bashrc file, assuming that your Windows Environment variables are set correctly and WSL is installed:

# Shared environment variables
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10

Just ensure that you change the directory to point to your JDK folder. In my case, it's in D:\Java\jdk11.0.4_10 which in WSL is /mnt/d/Java/jdk11.0.4_10

Also, since you're using Windows binaries, you must specify the file type when running from a WSL bash shell:

Example

Calling Windows JDK from:

CMD:

javac MyClass.java
java MyClass

WSL:

javac.exe MyClass.java
java.exe MyClass

Note WSL requires .exe since it is a Windows binary. When using the Linux distro of the OpenJDK, no extension is required..

Mayflower answered 24/7, 2020 at 14:11 Comment(0)
A
2

This worked for me. Though, I don't like adding these weird repositories. Linuxuprising? Would be great if Microsoft would fix that.

sudo add-apt-repository ppa:linuxuprising/java
sudo apt update
sudo apt install oracle-java10-installer
Aquilar answered 14/6, 2018 at 18:35 Comment(0)
G
1

it may work for Insider Preview build 14905, but hangs on Windows 10 Pro build 14393. A good part of the problem is that Microsoft is using Ubuntu 14.0.4. 14.10 or later would probably better support Java 8.

Gimbals answered 25/9, 2016 at 18:45 Comment(0)
U
1

This has referred to all above answers and comments and tries to put it together as a complete guidance.

It is straight forward to have jdk installed directly in WSL2 Linux therefore we skip this option here.

It is perfectly okay to use/share the Windows JDK with WSL2 Linux, you just need to setup two things, JAVA_HOME and PATH in your Linux shell profile, in my case, ~/.bashrc.

STEP 1: check your Windows Java location

By default it is installed in here (version can be different) C:\Program Files\Java\jdk1.8.0_321

WSL2 will be able to access to this Windows file location in such format /mnt/c/Program Files/Java/jdk1.8.0_321

Therefore,

STEP 2: load this with your shell profile,

Edit this file,

$ sudo nano ~/.bashrc

by adding following to its bottom

export JAVA_HOME="/mnt/c/Program Files/Java/jdk1.8.0_321"
export PATH=$JAVA_HOME/bin:$PATH
export alias java='java.exe'
export alias javac='javac.exe'

save it by Ctrl/CMD + O then Enter

Exit nano editor by Ctrl/CMD + X

Refresh the profile to load added variables by

$ source ~/.bashrc

There you go. You can now verify it is working by

$ java -version

It will give you something similar to following,

java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

Summary

You have done three things in total here,

  1. Defined your $JAVA_HOME as using the Windows installation of jdk

to verify this:

$ echo $JAVA_HOME
/mnt/c/Program Files/Java/jdk1.8.0_321
  1. Defined a user scope $PATH variable to let apps know where to find java compiler

to verify this:

$ echo $PATH
/mnt/c/Program Files/Java/jdk1.8.0_321/bin:...
  1. Set up alias to call java like you having it directly on Linux by call java instead of java.exe

to verify this:

$ java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)

Unfortunately this will make you do something like which java. In order to do this as for native Linux software, you might want to follow further steps like in here (not verified): https://mcmap.net/q/225912/-error-java_home-is-not-defined-correctly-executing-maven You however really don't need this as this won't do anything more than telling you where is your java that was borrowed from Windows.

Undertint answered 3/3, 2022 at 23:24 Comment(1)
it doesnt work.Respiration
A
0

It seems that the problem occured with java oracle version, I have download the openJDK version and now it work

Aeciospore answered 3/8, 2016 at 16:0 Comment(1)
Hum In fact probably come from the version of java, Java 7 work but Java 8 didn't.Aeciospore
W
-1

I had the same problem but I solve it with one command:

sudo apt upgrade

then run these 3 commands:

sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-8-jre-headless
Woodward answered 9/10, 2019 at 12:7 Comment(1)
The question specifically asks for Oracle JDK, not OpenJDK.Thereat

© 2022 - 2024 — McMap. All rights reserved.