I have a script called foo.sh in my home folder.
When I navigate to this folder, and enter ./foo.sh, I get
-bash: ./foo.sh: Permission denied.
When I use sudo ./foo.sh, I get
sudo: foo.sh: command not found.
Why does this happen and how I can fix it?
Permission denied
In order to run a script the file must have an executable permission bit set.
In order to fully understand Linux file permissions you can study the documentation for the chmod command. chmod, an abbreviation of change mode, is the command that is used to change the permission settings of a file.
To read the chmod documentation for your local system , run man chmod or info chmod from the command line. Once read and understood you should be able to understand the output of running ...
ls -l foo.sh
... which will list the READ, WRITE and EXECUTE permissions for the file owner, the group owner and everyone else who is not the file owner or a member of the group to which the file belongs (that last permission group is sometimes referred to as "world" or "other")
Here's a summary of how to troubleshoot the Permission Denied error in your case.
$ ls -l foo.sh # Check file permissions of foo
-rw-r--r-- 1 rkielty users 0 2012-10-21 14:47 foo.sh
^^^
^^^ | ^^^ ^^^^^^^ ^^^^^
| | | | |
Owner| World | |
| | Name of
Group | Group
Name of
Owner
Owner has read and write access rw but the - indicates that the executable permission is missing
The chmod command fixes that. (Group and other only have read permission set on the file, they cannot write to it or execute it)
$ chmod +x foo.sh # The owner can set the executable permission on foo.sh
$ ls -l foo.sh # Now we see an x after the rw
-rwxr-xr-x 1 rkielty users 0 2012-10-21 14:47 foo.sh
^ ^ ^
foo.sh is now executable as far as Linux is concerned.
Using sudo results in Command not found
When you run a command using sudo you are effectively running it as the superuser or root.
The reason that the root user is not finding your command is likely that the PATH environment variable for root does not include the directory where foo.sh is located. Hence the command is not found.
The PATH environment variable contains a list of directories which are searched for commands. Each user sets their own PATH variable according to their needs.
To see what it is set to run
env | grep ^PATH
Here's some sample output of running the above env command first as an ordinary user and then as the root user using sudo
rkielty#rkielty-laptop:~$ env | grep ^PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
rkielty#rkielty-laptop:~$ sudo env | grep ^PATH
[sudo] password for rkielty:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
Note that, although similar, in this case the directories contained in the PATH the non-privileged user (rkielty) and the super user are not the same.
The directory where foo.sh resides is not present in the PATH variable of the root user, hence the command not found error.
The other solutions I've seen here so far are based on some system definitions, but it's in fact possible to have sudo use the current PATH (with the env command) and/or the rest of the environment (with the -E option) just by invoking it right:
sudo -E env "PATH=$PATH" <command> [arguments]
In fact, one can make an alias out of it:
alias mysudo='sudo -E env "PATH=$PATH"'
(It's also possible to name the alias itself sudo, replacing the original sudo.)
Check for secure_path on sudo
[root#host ~]# sudo -V | grep 'Value to override'
Value to override user's $PATH with: /sbin:/bin:/usr/sbin:/usr/bin
If $PATH is being overridden use visudo and edit /etc/sudoers
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
Check that you have execute permission on the script. i.e. chmod +x foo.sh
Check that the first line of that script is #!/bin/sh or some such.
For sudo you are in the wrong directory. check with sudo pwd
You can also create a soft link to your script in one of the directories (/usr/local/bin for example) in the super user PATH. It'll then be available to the sudo.
chmod +x foo.sh
sudo ln -s path-to-foo.sh /usr/local/bin/foo
Have a look at this answer to have an idea of which directory to put soft link in.
It seems that linux will say "command not found" even if you explicitly give the path to the file.
[veeam#jsandbox ~]$ sudo /tmp/uid.sh;echo $?
sudo: /tmp/uid.sh: command not found
1
[veeam#jsandbox ~]$ chmod +x /tmp/uid.sh
[veeam#jsandbox ~]$ sudo /tmp/uid.sh;echo $?
0
It's a somewhat misleading error, however it's probably technically correct. A file is not a command until its executable, and so cannot be found.
Try chmod u+x foo.sh instead of chmod +x foo.sh if you have trouble with the guides above. This worked for me when the other solutions did not.
Regarding "command not found" when using sudo far less hackier way would be to edit secure_path.
It is perfectly described here:
https://superuser.com/questions/927512/how-to-set-path-for-sudo-commands
Ok this is my solution:
in ~/.bash_aliases just add the following:
# ADDS MY PATH WHEN SET AS ROOT
if [ $(id -u) = "0" ]; then
export PATH=$PATH:/home/your_user/bin
fi
Voila!
Now you can execute your own scripts with sudo or set as ROOT without having to do an export PATH=$PATH:/home/your_user/bin everytime.
Notice that I need to be explicit when adding my PATH since HOME for superuser is /root
If you are not so comfortable with the command line and are using Ubuntu you can solve the problem as follows:
Open the folder window where the file is located
Right click on the executable file and choose Properties
Go to the Permissions tab and highlight Allow executing file as program
With this solution you allow the user to execute the file as a program and you don't need sudo (or change the PATH environment variable for root).
It seems sudo command not found
to check whether the sudo package is installed on your system, type sudo , and press Enter . If you have sudo installed the system will display a short help message, otherwise you will see something like sudo: command not found
To install sudo, run one of the following commands using root account:
apt-get install sudo # If your system based on apt package manager
yum install sudo # If your system based on yum package manager
Related
I just installed JDK in Ubuntu with sudo apt-get install openjdk-6-jdk command,
after the installation where's the Java bin directory located? And how can I set the environment path for that directory? I have little experience with Ubuntu, can anyone give some advice or suggest any good website for reference?
set environment variables as follows
Edit the system Path file /etc/profile
sudo gedit /etc/profile
Add following lines in end
JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Then Log out and Log in ubuntu for setting up the paths...
Java is typically installed in /usr/java
locate the version you have and then do the following:
Assuming you are using bash (if you are just starting off, i recommend bash over other shells) you can simply type in bash to start it.
Edit your ~/.bashrc file and add the paths as follows:
for eg. vi ~/.bashrc
insert following lines:
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
after you save the changes, exit and restart your bash or just type in bash to start a new shell
Type in export to ensure paths are right.
Type in java -version to ensure Java is accessible.
Ubuntu installs openjdk6 to /usr/lib/jvm/java-6-openjdk path. So you will have the bin in /usr/lib/jvm/java-6-openjdk/bin. Usually the classpath is automatically set for the java & related executables.
To Set JAVA_HOME / PATH for a single user, Login to your account and open .bash_profile file
$ vi ~/.bash_profile
Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java>. If your path is set to /usr/java/jdk1.5.0_07/bin/java, set it as follows:
export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
Set PATH as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Feel free to replace /usr/java/jdk1.5.0_07 as per your setup. Save and close the file. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:
$ source ~/.bash_profile
OR
$ . ~/.bash_profile
Verify new settings:
$ echo $JAVA_HOME
$ echo $PATH
Tip: Use the following command to find out exact path to which java executable under UNIX / Linux:
$ which java
Please note that the file ~/.bashrc is similar, with the exception that ~/.bash_profile runs only for Bash login shells and .bashrc runs for every new Bash shell.
To Set JAVA_HOME / PATH for all user, You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
# vi /etc/profile
Next setup PATH / JAVA_PATH variables as follows:
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
export PATH=$PATH:/usr/java/jdk1.5.0_07/bin
Save and close the file. Once again you need to type the following command to activate the path settings immediately:
# source /etc/profile
OR
# . /etc/profile
You need to set the $JAVA_HOME variable.
In my case while setting up Maven, I had to set it up to where JDK is installed.
First find out where JAVA is installed:
$ whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Now dig deeper:
$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 46 Aug 25 2018 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Dig deeper:
$ ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
-rwxr-xr-x 1 root root 6464 Mar 14 18:28 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
As it is not being referenced to any other directory, we'll use this.
Open /etc/environment using nano:
$ sudo nano /etc/environment
Append the following lines
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export JAVA_HOME
Reload PATH using:
$. /etc/environment
Now:
$ echo $JAVA_HOME
Here is your output:
/usr/lib/jvm/java-1.8.0-openjdk-amd64
Sources I referred to:
https://askubuntu.com/a/175519
https://stackoverflow.com/a/23427862/6297483
if you have intalled only openJDK, the you should update your links, because you can have some OpenJDK intallation.
sudo update-alternatives --config java
after this
$gedit ~/.bashrc
add the following line in the file
JAVA_HOME=/usr/lib/jvm/YOUR_JAVA_VERSION
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
you can get you java version with
java -version
Open terminal (Ctrl+Alt+t)
Type
sudo gedit .bashrc
Enter password of ubuntu user
Go to last line of the file
Type below code in new line
export JAVA_HOME=enter_java_path_here
export PATH=$JAVA_HOME/bin:$PATH
eg: export JAVA_HOME=/home/pranav/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
Save the file
Type
source ~/.bashrc
in terminal
Done
To set up system wide scope you need to use the
/etc/environment file sudo gedit /etc/environment
is the location where you can define any environment variable. It can be visible in the whole system scope. After variable is defined system need to be restarted.
EXAMPLE :
sudo gedit /etc/environment
Add like following :
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/opt/jdk1.6.0_45/"
Here is the site you can find more : http://peesquare.com/blogs/environment-variable-setup-on-ubuntu/
How to install java packages:
Install desired java version / versions using official ubuntu packages, which are managed using alternatives:
sudo apt install -y openjdk-8-jdk
or/and other version:
sudo apt install -y openjdk-11-jdk
Above answers are correct only when you have only one version for all software on your machine, and you can skip using update-alternatives. So one can quickly hardcode it in .bashrc or some other place:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
but it's not healthy, as later on you may change the version.
Correct way to set JAVA_HOME (and optionally JAVA_SDK, JAVA_JRE )
The correct way (and mandatory when you have more than one), is to detect what update-alternative is pointing to, and always use update-alternatives to switch active version.
Here are the suggestions for both: only specific unix account or for all accounts (machine level).
1. for a specific unix account only:
Use this if you don't have permissions to do it at machine level.
cat <<'EOF' >>~/.bashrc
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
2. To do it at machine level, and for all bourne shells, you need 2 steps:
2.a
cat <<'EOF' | sudo tee /etc/profile.d/java_home_env.sh >/dev/null
export JAVA_HOME=$(update-alternatives --query java | grep Value | cut -d" " -f2 | sed 's!\(\/.*\)jre\(.*\)!\1!g')
export JDK_HOME=${JAVA_HOME}
export JRE_HOME=${JDK_HOME}/jre/
EOF
As your shell might not be set as interactive by default, you may want to do this also:
2.b
cat <<'EOF' | sudo tee -a /etc/bash.bashrc >/dev/null
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
EOF
PS: There should be no need to update the $PATH, as update-alternatives takes care of the link to /usr/bin/.
More on: https://manpages.ubuntu.com/manpages/trusty/man8/update-alternatives.8.html
It should put java in your path, probably in /usr/bin/java. The easiest way to find it is to open a term and type which java.
Create/Open ~/.bashrc file $vim ~/.bashrc
Add JAVA_HOME and PATH as referring to your JDK path
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Save file
Now type java -version it should display what you set in .bashrc file.
This will persist over sessions as well.
Example :
Update bashrc file to add JAVA_HOME
sudo nano ~/.bashrc
Add JAVA_HOME to bashrc file.
export JAVA_HOME=/usr/java/<your version of java>
export PATH=${PATH}:${JAVA_HOME}/bin
Ensure Java is accessible
java -version
In Case of Manual installation of JDK, If you got an error as shown below
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Execute the following command in your JAVA_HOME/lib directory:
unpack200 -r -v -l "" tools.pack tools.jar
Execute the following commands in your JAVA_HOME/jre/lib
../../bin/unpack200 rt.pack rt.jar
../../bin/unpack200 jsse.pack jsse.rar
../../bin/unpack200 charsets.pack charsets.jar
Ensure Java is accessible
java -version
I have a Linux Lite 3.8 (It bases on Ubuntu 16.04 LTS) and a path change in the following file (with root privileges) with restart has helped.
/etc/profile.d/jdk.sh
Step1:
sudo gedit ~/.bash_profile
Step2:
JAVA_HOME=/home/user/tool/jdk-8u201-linux-x64/jdk1.8.0_201
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
Step3:
source ~/.bash_profile
Let me simplify:
download JDK from Oracle Website : Link
Extract it
Create a folder (jvm) in /usr/lib/ i.e /usr/lib/jvm
move the extracted folder from the jdk to /usr/lib/jvm/
*Note : use terminal, sudo, mv command i.e. sudo mv
Create a .sh file at /etc/profile.d/ eg: /etc/profile.d/myenvvar.sh
In the .sh file type
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin
*Note : use terminal, gedit and sudo eg: sudo gedit myenvvar.sh
Turn Off the Computer, after all these steps and Restart it
Open Terminal , and type
java -version
Check the output , then type
echo $JAVA_HOME
Once I've installed the openjdk version of the Java Development Kit on an Ubuntu machine, I use this procedure to create a JAVA_HOME environment variable that doesn't need to be changed after every version upgrade of the openjdk installation.
Firstly, I issue a command to discover the directory in which the java executables are located for this java installation.
echo $(readlink -e `which java` | xargs -0 dirname)
If I'm happy with the output from that, everything else can be derived from it.
Configuring the JAVA_HOME and PATH environment variables
Rather than adding more and more configurations to the ~/.bashrc file I've found it a cleaner practice to create a separate small file that ~/.bashrc can "include" when it runs.
Let's call that file ~/.java_env_vars (but you could name it whatever you wish).
Add an "include condition" to the ~/.bashrc file
Open ~/.bashrc in any text editor and these lines to the end of the file:
# include the java environment configuration file here (if it exists)
if [ -f "$HOME/.java_env_vars" ]; then
. $HOME/.java_env_vars
fi
Create the Java environment variable configuration file
Open any text editor, create the Java environment configuration file ~/.java_env_vars and add this content to it:
#1. set a java_bin variable to the directory containing the actual Java executables.
java_bin=$(readlink -e `which java` | xargs -0 dirname)
#2. append "$java_bin" to the PATH environment variable
export PATH=$PATH:"$java_bin"
#3. assign the directory of the current Java installation to the JAVA_HOME environment variable.
export JAVA_HOME=$(dirname "$java_bin")
NOTE: exporting the PATH and JAVA_HOME variables just ensures that they're always available wherever they're needed.
Source your ~/.bashrc file (so that the changes you made to it are reflected) using the following command:
source ~/.bashrc
Test the Java environment variable configuration
Open a new terminal console, and test the new Java environment variables by issuing this command:
echo "$PATH" && echo "$JAVA_HOME"
Output should be two lines, something like this:
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-11-openjdk-amd64/bin
/usr/lib/jvm/java-11-openjdk-amd64
All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.
echo $JAVA_HOME
Open file /etc/environment with a text editor
Add the line JAVA_HOME="[path to your java]"
Save and close then run source /etc/environment
Set java version from the list of installed. For see the list of the installed version run following command:
update-java-alternatives --list
Then set your java version according to the following command:
sudo update-java-alternatives --set /usr/lib/jvm/java-1.8.0-openjdk-amd64
open jdk once installed resides generally in your /usr/lib/java-6-openjdk
As usual you would need to set the JAVA_HOME, classpath and Path.
In ubuntu 11.04 there is a environment file available in /etc where you need to set all the three paths. And then you would need to restart your system for the changes to take effect..
Here is a site to help you around
http://aliolci.blogspot.com/2011/05/ubuntu-1104-set-new-environment.html
Once JDK installed set the JAVA_HOME in environment
sudo nano /etc/environment and add the line JAVA_HOME="/usr/lib/jvm/jdk-11.0.1/"
Add the configuration in .bashrc
sudo nano ~/.bashrc and add following lines
JAVA_HOME=/usr/lib/jvm/jdk-11.0.11/
PATH=$JAVA_HOME/bin:$PATH
refresh the new configuration with source ~/.bashrc
enter the command java -version and you can see the version installed on your machine
You can install the default Ubuntu(17.10) java from apt:
sudo apt install openjdk-8-jdk-headless
And it will set the PATH for you, if instead you need to install specific version of Java you can follow this YouTube
I installed java 11 in my Ubuntu 20.04. Setting up a JAVA_HOME for the same.
enter the this command to find out your ubuntu version --
swapnil#swapnil-vm:~$ lsb_release -d
Description: Ubuntu 20.04.3 LTS
enter this command to find out the location of your jvm --
swapnil#swapnil-vm:~$ whereis jvm
jvm: /usr/lib/jvm
open .bashrc in any editor of your choice --
nano .bashrc
add the following lines --
## setting JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
now you are good to go!!
open a new terminal and enter the command --
ehco $JAVA_HOME
Use the following lines to set the path variables in the /etc/environment
echo export JAVA_HOME=/path/to/java | sudo tee -a /etc/environment
echo export JRE_HOME=/path/to/jre | sudo tee -a /etc/environment
It should work.
Note:
You should reboot the system for changes to take effect.
Installation of Oracle Java:
Download the tarball (.tar file) from Oracle website
unzip it by sudo tar -xvpzf fileName -C /installation_folder_name
change the files permission and ownership
add the following two lines in /etc/profile
export JAVA_HOME=/home/abu/Java/jdk1.8.0_45/
export PATH=$JAVA_HOME/bin:$PATH
restart the machine and check by java -version and javac -version
First, check whether env var exists or not
echo $JAVA_HOME
if an env var exists with that name then the above command will return the env var Path. if it's return nothing then copy the env path first then execute below command. such as my java env path is /usr/lib/jvm/java-11-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
I am new to Linux system and there seem to be too many Java folders.
java -version gives me:
java version "1.7.0_55"
OpenJDK Runtime Environment (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
When I am trying to build a Maven project , I am getting error:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java
Could you please tell me which files I need to modify for root as well as not-root user and where exactly is java located?
find /usr/lib/jvm/java-1.x.x-openjdk
vim /etc/profile
Prepend sudo if logged in as not-privileged user, ie. sudo vim
Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.
You could use /etc/profile or better a file like /etc/profile.d/jdk_home.sh
export JAVA_HOME=/usr/java/jdk1.7.0_05/
You have to remember that this file is only loaded with new login shells.. So after bash -l or a new gnome-session and that it doesn't change with new Java versions.
None of the other answers were "sticking" for me in RHEL 7, even setting JAVA_HOME and PATH directly in /etc/profile or ~/.bash_profile would not work. Each time I tried to check if JAVA_HOME was set, it would come up blank:
$ echo $JAVA_HOME
(<-- no output)
What I had to do was set up a script in /etc/profile.d/jdk_home.sh:
#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH
I initially neglected the first line (the #!/bin/sh), and it won't work without it.
Now it's working:
$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/
Open terminal and type sudo gedit .bashrc
It will ask you your password. After typing the password, it will open the bash file. Then go to end and type:
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Then save the file and exit from file
Above is for a single user. For all users, you have to follow below steps
gedit /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
export PATH=$PATH:$JAVA_HOME/bin
Copy the bin file path you installed
YOUR PATH
open terminal and edit environment file by typing following command,
sudo nano /etc/environment
In this file, add the following line (replacing YOUR_PATH by the just copied path):
JAVA_HOME="YOUR_PATH"
That should be enough to set the environment variable. Now reload this file:
source /etc/environment
now test it by executing:
echo $JAVA_HOME
Doing what Oracle does (as a former Sun Employee I can't get used to that one)
ln -s latestJavaRelease /usr/java/default
Where latestJavaRelease is the version that you want to use
then export JAVA_HOME=/usr/java/default
The answer is given previous posts is valid. But not one answer is complete with respect to:
Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in
/etc/profile.d/ to make custom changes to your environment, as this
will prevent the need for merging in future updates.*
So as stated above create /etc/profile.d/custom.sh file for custom changes.
Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:
#if making jdk as java home
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
OR
#if making jre as java home
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
And remember to have #! /bin/bash on the custom.sh file
First you need to find out which Java is installed in your PC and which one to use.
For that open terminal with root permission.
sudo su
ls /usr/lib/jvm/
Now it will list the available java versions.
Select the listed version.
Copy the path till there.
Now open bashrc
nano ~/.bashrc
add the following commands to the end
export JAVA_HOME="path that you copied"
export PATH=$JAVA_HOME/bin:$PATH
after that save the file and exit by pressing Ctrl+S followed by Ctrl+X
Now run the below command:
source ~/.bashrc
1...Using the short cut Ctlr + Alt + T to open terminal
2...Execute the below command:
echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null
3...(Recommended) Restart your VM / computer. You can use source /etc/source if don't want to restart computer
4...Using the short cut Ctlr + Alt + T to open terminal
5...Verified JAVA_HOME installment with
echo $JAVA_HOME
One-liner copy from flob, credit to them
This is a very simple script to solve the problem
export JAVA_HOME_BIN=`which java`
export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN`
export JAVA_HOME=`dirname $JAVA_HOME_DIR`
And for testing:
echo $JAVA_HOME
Posting as answer, as I don't have the privilege to comment.
Point to note: follow the accepted answer posted by "That Dave Guy".
After setting the variables, make sure you set the appropriate permissions to the java directory where it's installed.
chmod -R 755 /usr/java
All operational steps(finding java, parent dir, editing file,...) one solution
zFileProfile="/etc/profile"
zJavaHomePath=$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
echo $zJavaHomePath
echo "export JAVA_HOME=\"${zJavaHomePath}\"" >> $zFileProfile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> $zFileProfile
Result:
# tail -2 $zFileProfile
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
export PATH=$PATH:$JAVA_HOME/bin
Explanation:
1) Let's break the full command into pieces
$(readlink -ze $(which java) | xargs -0 dirname | xargs -0 dirname)
2) Find java path from java command
# $(which java)
"/usr/bin/java"
3) Get relative path from symbolic path
# readlink -ze /usr/bin/java
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java"
4) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/java
# readlink -ze /usr/bin/java | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin"
5) Get parent path of /usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64/bin/
# readlink -ze /usr/bin/java | xargs -0 dirname | xargs -0 dirname
"/usr/lib/jvm/java-11-openjdk-11.0.7.10-1.el8_1.x86_64"
Step 1 - check the current java version by "echo $JAVA_HOME"
Step 2 - vim /etc/profile
Step 3 - At the end of file you will find
export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.
Step 4 - Save and exit :wq
Step 5 - "source /etc/profile/", this would execute the change
Step 6 - Again do a echo $JAVA_HOME - change would have been reflected.
Probably a good idea to source whatever profile you edit to save having to use a fresh login.
either:
source /etc/
or
. /etc/
Where is whatever profile you edited.
On Linux I add this line to my ~/.profile:
export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)
While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:
1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.
2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.
3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.
Read more: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH
Use SDKMAN sdkman.io to switch btw. your sdk's.
It sets the JAVA_HOME for you.
open kafka-run-class.sh with sudo to write
you can find kafka-run-class.sh in your kafka folder : kafka/bin/kafka-run-class.sh
check for these lines
Modify the JAVA variable in the else part to point to the java executable in your java/bin. like JAVA="$JAVA_HOME/java"
In /etc/profile , if you open that will you’ll get to know that IT IS no recommended to write on that file. Instead of that make a script of your commands(suppose test.sh)go to /etc/profile.d folder and Put test.sh there. Every time you instance reboot it’ll be automatically called by /etc/profile.
Using vim might be a bit difficult for new user. We can use gedit text editor instead.
Find /usr/lib/jvm/java-1.x.x-openjdk
Enter "gedit /etc/profile" or use "sudo gedit /etc/profile" if logged in as not-privileged
Add the following at the end of line:
export JAVA_HOME="path that you found"
export PATH=$JAVA_HOME/bin:$PATH
Enter "source /etc/profile" in your current shell to apply the changes
I use the line:
export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../)
to my ~/.profile so it uses the base of the default java directory at login time. This is for bash.
Try this if doesn't work:
apt install openjdk-8-jdk-headless
Here is my executable file:
#!/bin/bash
JAVA_HOME=/cad2/ece419s/java/jdk1.6.0/
# arguments to GameServer
# $1 = port # where GameServer is listening
${JAVA_HOME}bin/java GameServer $1
I run the chmod command as:
chmod +x server.sh
And then:
./ server.sh 8000
8000 is the port from local computer which my server is listening to.
After bash -x server.sh 8000, I got
It looks like you write this server.sh script on other platform (maybe Windows) and copy it to a Linux system, and these two systems use different character for end-of-line, so try to convert it to Linux format first by dos2unix server.sh.
If your system does not have dos2unix, you can install it by
on Fedora yum install dos2unix
on Debian apt-get install dos2unix
Both need root privilege.
If you cannot or does not want to install dos2unix, another option is using vim (it should be avariable in a normal Linux machine):
vim server.sh
:set ff=unix
:w
It looks like there is a space between ./ and server.sh so you are trying to execute ./.
You can run command "ls -l" by going in previous directory and check whether the permissions are applied to the file or not.
If not then check for the user whether he has change permissions or not. Else you can use "root" user who mostly has all the permissions.
I'm not good with the terminal, so I have no idea what this means....
you should add the framework installation directory to your system PATH. On UNIX systems, this means doing something like:
export PATH=$PATH:/path/to/play20
On Windows you’ll need to set it in the global environment variables. This means update the PATH in the environment variables and don’t use a path with spaces.
If you're on UNIX, make sure that the play script is executable (otherwise do a chmod a+x play).
Can someone guide me through these steps. I have the Play 2.0 Folder placed in my /Documents.
I would suggest installing Play with Brew if using OS X on Mac.
First install Brew
ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
Then install Play
brew install play
Open .bash_profile
vi ~/.bash_profile
Ensure that /usr/local/bin is in your path by updating or adding the PATH line to something like this :
PATH=/usr/local/bin:$PATH
Open a new terminal window, and Play should be on your path.
You may also want to install (via brew) Scala, SBT etc.
This is what I did:
Download the play framework, extracted it to my desktop, open up iterm and vi ~/.bash_profile
add path: export PATH=$PATH:/Users/*your-username*/Desktop/play-2.2.0 save the file (that's press esc and type :wq [save and quit]) reload your bash_profile, just type source ~/.bash_profile cd to your directory and type play and your done.
Hope that's helpful.
In export PATH=$PATH:/path/to/play20, /path/to/play20 is a placeholder for your real path, which should be /Users/ronyjohn007/Documents/play20 [drag the folder from Finder into Terminal window for the actual name] - so type:
export PATH=$PATH:/Users/ronyjohn007/Documents/play20
This tells your shell to look into such folder for executables. Then:
chmod +x /Users/ronyjohn007/Documents/play20/play
This gives play file execution permission. Now close and reopen Terminal, and type play followed by enter.
You can also use symlink(s) in /usr/bin to point exact file, this is usefull, when you're using more than one version of play on the system
BTW (I can see that you performed reverse operation in your previous question :) -removed the previous: /usr/bin/play command )
(paste each line separately and confirm):
sudo -i
cd /usr/bin
chmod +x /Users/ronyjohn007/Documents/play-2.0.3/play
ln -ls /Users/ronyjohn007/Documents/play-2.0.3/play play
exit
Other sample - git master in this case
# this creates new folder in your docs,
# and clones current master version of Play from GitHub to
# /Users/ronyjohn007/Documents/play-from-github/Play20 folder
cd ~/Documents
mkdir play-from-github
cd play-from-github/
git clone https://github.com/playframework/Play20.git
# this sets alternative command as in sample 1
sudo -i
cd /usr/bin
chmod +x /Users/ronyjohn007/Documents/play-from-github/Play20/play
ln -ls /Users/ronyjohn007/Documents/play-from-github/Play20/play play-master
exit
Finally now you can check which command points to which version:
ls -la /user/bin | grep play
should give something like:
... play -> /Users/ronyjohn007/Documents/play-2.0.3/play
... play-master -> /Users/ronyjohn007/Documents/play-from-github/Play20/play
after terminal reopen they both should be available as common commands.
Note: of course you can't use play command with applications created with play-master new ... and vice-versa!
You can edit your ~/.profile file and add:
export PLAY_HOME=your-play-directory
export PATH=$PATH:$PLAY_HOME
Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.
On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))
On Mac you can run $(dirname $(readlink $(which javac)))/java_home
I'm not sure about windows but I imagine where javac would get you pretty close
Just another solution, this one's cross platform (uses java), and points you to the location of the jre.
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Outputs all of java's current settings, and finds the one called java.home.
For windows, you can go with findstr instead of grep.
java -XshowSettings:properties -version 2>&1 | findstr "java.home"
echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.
If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.
I know this is late, but this command searches the /usr/ directory to find java for you
sudo find /usr/ -name *jdk
Results to
/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk
FYI, if you are on a Mac, currently JAVA_HOME is located at
/System/Library/Frameworks/JavaVM.framework/Home
To show the value of an environment variable you use:
echo $VARIABLE
so in your case will be:
echo $JAVA_HOME
In case you don't have it setted, you can add in your .bashrc file:
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
and it will dynamically change when you update your packages.
If $JAVA_HOME is defined in your environment...
$ echo $JAVA_HOME
$ # I am not lucky...
You can guess it from the classes that are loaded.
$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]
This method ensures you find the correct jdk/jre used in case there are multiple installations.
Or using strace:
$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
On the Terminal, type:
echo "$JAVA_HOME"
If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.
Did you set your JAVA_HOME
Korn and bash shells:export JAVA_HOME=jdk-install-dir
Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
C shell:setenv JAVA_HOME jdk-install-dir
Here's an improvement, grabbing just the directory to stdout:
java -XshowSettings:properties -version 2>&1 \
| sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3 /usr/java/jdk-17.0.2/bin/java
Enter to keep the current selection[+], or type selection number:
from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2
http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment
If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.