Installing Oracle JDK on Windows subsystem for Linux

前端 未结 12 1956
半阙折子戏
半阙折子戏 2020-12-12 12:34

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 at

12条回答
  •  北海茫月
    2020-12-12 13:10

    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!

提交回复
热议问题