0

I'm trying to set my java environment variable but I am new to Ubuntu so don't know how to update it through Vi. I want to do the following things:

Edit the environment file

# vi /etc/environment

Update the existing PATH variable by adding the below bin folders, separated with a colon:

/usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin

HOME directory paths can be different based on version and update,here the version is 1.8 and the update is 151. Add the below variables at the end of environment file, making changes for your specific version and update.

J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151"
J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"

The environment file should now be similar to this text:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_151/bin:/usr/lib/jvm/jdk1.8.0_151/db/bin:/usr/lib/jvm/jdk1.8.0_151/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_151"
J2REDIR="/usr/lib/jvm/jdk1.8.0_151/jre*
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_151"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_151/db"

My problem is I don't know much about editing and saving in Vi; I tried to do it through some resources but then it start giving warning and I don't want to corrupt things so I thought I should ask for help from some experienced users, so kindly help me through guiding step by step on how to do above things in Vi through terminal. I am using Ubuntu 16.04 LTS. Thank you!

2
  • 3
    you do not have to use Vi. You can use any text editor, for example run sudo nano if you know nano. To edit and save a file using Vi(m), see How do I save files edited with vim?
    – Zanna
    Commented Apr 15, 2018 at 10:49
  • 1
    @Zanna Thank you so much for telling me ,through sudo nano it was quiet easy :)
    – user716691
    Commented Apr 15, 2018 at 11:11

1 Answer 1

2

Vim steps would be:

  1. From your terminal open document for editing with commands:

    • sudo vi /etc/environment
    • Note that the sudo is needed since only users with elevated priveleges can edit files in the /etc folder
  2. To start editing the file from your keyboard use the following key combinations:

    1. Start to edit: i
    2. To move around, use keys h, j, k, and l
  3. After edit to get out of edit mode [ which was entered with i ], use esc

  4. After exiting to save: Shift + ;, then [wq | x], now press Enter

These are just basic steps, others exist so search and read up.

Quick tips:

  1. Always use the esc to get out of vim modes.

  2. I wrote something but don't want to keep it (provided you didn't do esc, Shift+;, w, Enter), use esc, then Shift+;, then q followed by !, then finally Enter.

  3. Finished want to save and quit at the same time, use esc, then Shift+;, then x, now finish with Enter

6
  • Thank you so much for your detailed response , I have a further query after updating I was using update-alternatives to inform ubuntu about installed java paths and it gave me following response : update-alternatives: using /usr/lib/jvm/jdk1.8.0_162/bin/java to provide /usr/bin/java (java) in auto mode So is it all good ,I am actually doing it first time so asking thank you !
    – user716691
    Commented Apr 15, 2018 at 11:15
  • and my initial command for update-alternatives was following: sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_162/bin/javac" 0
    – user716691
    Commented Apr 15, 2018 at 11:18
  • Use this to see what you have: update-alternatives --list java Commented Apr 15, 2018 at 11:41
  • thanks ,i did that already and its showing /usr/lib/jvm/jdk1.8.0_162/bin/java Actually was confused when it said "in auto mode" so suspected as if I'm wrong somewhere because don't know what auto mode really is ,that's why asked .
    – user716691
    Commented Apr 15, 2018 at 11:48
  • Please ask a fresh question! Commented Apr 15, 2018 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .