install jre in a non interactive script - java

I want to install jre 1.7 in a non interactive script.
is there a way to do that in equivalent of the following?
update-alternatives --config java
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 /usr/lib/jvm/jre-1.5.0-gcj/bin/java
*+ 2 /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
3 /usr/lib/jvm/jre1.7/bin/java

Have a look at man update-alternatives. This reads:
--set name path
Set the program path as alternative for name.
This is equivalent to --config but is non-interactive
and thus scriptable.

Related

Java has too many level symbolic links

Well,I was trying to install forge on my linux machine(Kali-Linux).While on the was I was and still am unable to access the forge file(.jar).Due to an issue in which there is a lack of a command called "java".
The current problem I am having is sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.1/bin/java 1 while using this command I get a response as update-alternatives: error: cannot stat file '/bin/java': Too many levels of symbolic links.
When I try to ls in the /bin.The java symbolic link has a weird symbol(Just wanted to mention it cause it looked weird)
Is there anyway to fix this problem?
The "too many levels of symbolic links" message means that 40 symlinks have been traversed in attempting to resolve a path to an object.
It nearly always happens because you have a symlink that directly or indirectly points to itself. And the purpose of the limit (and the check) is to prevent the kernel going into an infinite loop.
So, why it is it happening here? Well, it is not entirely clear. But it seems like an earlier update-alternatives run has left some "wreckage" in the form of bad symlinks. If "/bin/java" already points to "/etc/alternatives/java", then "/etc/alternatives/java" should be a symlink to ... somewhere else.
Try the following:
Use update-alternatives --remove-all java to clear out any alternatives for the java command. (If this fails with the same problem, try step 2 first, then retry this step. Adding --force might help too.)
Remove the "/bin/java" and "/usr/bin/java" symlinks if they still exist.
Make sure that you have actually installed Java (already!), and "/usr/lib/jvm/jdk-16.0.1/bin/java" is the correct path for the executable. Run /usr/lib/jvm/jdk-16.0.1/bin/java -version to check.
Run sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.1/bin/java 1.
Note that update-alternatives does not actually install anything. For the command to work properly, you need to have installed (in this case) a Java JRE or JDK or a Kali Java package already.
Note that it cannot do any real harm to remove "alternatives" symlink chains that are broken. You can always clean them out and rebuild them. (By hand, if necessary.) The alternatives mechanism is just a fancy way of managing symlinks.
I found the solution. I'm putting it here with a clear explanation.
When you use this command sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.1/bin/java 1 to update java, there is already a symbolic-link of java file between these 3 directories /usr/bin/java -> /etc/alternatives/java and-> /lib/jvm/jdk-current-version/bin/java.
For updating alternatives first you need to remove the symbolic link at /lib/jvm/jdk-current-version/bin/java. Which will ultimately remove all links. Then you need to remove the java file from /usr/bin/java and /etc/alternatives/java.
Then run the update-alternatives command.
In my case, I did the following commands
rm symlink /usr/lib/jvm/java-11-openjdk-amd64/bin/java
update-alternatives --remove-all java for removing the java file.
rm /usr/bin/java if java file still exits in /usr/bin
then run the command update-alternatives --install /usr/bin/java java /opt/jdk/jdk-17.0.4.1+1/bin/java 100
or in your case it is sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.1/bin/java 1.
This will install new version of java into /usr/bin from the jdk directory you specified and automatically symbolic-links all the java files.

could not find java in JAVA_HOME at /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java/bin/java

I'm getting an error when setting the $JAVA_HOME variable on my CentOS system.
Not sure what causing Java to be unrecognized, this is a new VM provisioned with no issues observed.
[Khurum#localhost bin]$ sudo update-alternatives --config java
[sudo] password for Khurum:
There is 1 program that provides 'java'.
Selection Command
-----------------------------------------------
*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java)
Enter to keep the current selection[+], or type selection number: ^C
[Khurum#localhost bin]$ export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java
[Khurum#localhost bin]$ echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java
[Khurum#localhost bin]$ ./elasticsearch
could not find java in JAVA_HOME at /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/bin/java/bin/java
You are setting your JAVA_HOME incorrectly; it should point to the folder, not the Java executable:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/

-bash: java: command not found in Centos5

# java -version
-bash: java: command not found
I have jdk1.8.0_40 this folder in usr/local.
When I navigate to the folder and type alternatives --config jar, I get such output
There is 1 program that provides 'jar'.
Selection Command
-----------------------------------------------
*+ 1 /opt/jdk1.8.0_40//bin/jar
Enter to keep the current selection[+], or type selection number:
These are the content in jdk
bin include lib README.html THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT javafx-src.zip LICENSE release THIRDPARTYLICENSEREADME.txt
db jre man src.zip
You need to add your installation path to your PATH variable. Bash only looks for executables in the paths defined by your PATH variable.
You need to install java first:
alternatives --install /usr/bin/java java <path to java>
Change the var in above command to appropriate location of you java file, for example: /usr/local/jdk/bin/java
And then run the following command to set it as default java:
alternatives --config java

update-alternative too many symbolic links

I am trying to install Juniper Network connect 32 bit on my 64 bit Ubuntu 14.10. following the documentation here
Network connect needs the java 32 bit to be installed along with the 64 java.
those are some commands i used while installing java 64:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.8.0_31/bin/java" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.8.0_31/bin/javaws" 1
sudo update-alternatives --set javaws /usr/local/java/jre1.8.0_31/bin/javaws
Next I installed java 32 under this directory Usr/local/Java32/jre.8.0_31.
Im trying as written in the tutorial to make Network Connect link to Java32 instead of java64 while jave 64 is the default. so I'm using this command line:
sudo update-alternatives --install /usr/bin/java java /usr/java32/jre1.8.0_31/bin/java 10.
But it is not working! i tried it different times with different changes and ofcourse i got this message:
too many levels of symbolic links
how do i undo all the update-alternatives i made? and what is the right way to link to java 32?
Use this command to start network connect which i am reproducing from the same juniper's page (there is a bug in the command. use -jar & not -cp as given by them). please note the NC.jar sits in your $HOME/.juniper_networks/
And in the command, for give java 6 bin path (in your case, /usr/java32/hre1.8.0_31/bin/java)
<java_path> -jar NC.jar -h <ivehostname> -u <username> -p <password> [-r <realm>]
-f <ivecertificate_in_der_format> [-l <gui_log_level> [-L <ncsvc_log_level>] [-y <proxy> -
z <proxy_port> [-s <proxy_username> -a <proxy_password> [-d <proxy_domain>]]]

JAVA_HOME and PATH are set but java -version still shows the old one

I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOME and $PATH environment variables in ~/.profile as follows:
export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=/home/aqeel/development/jdk/jdk1.6.0_35/bin:$PATH
I then did source ~/.profile to make the proper changes.
When I execute java -version command to check the active java version, it shows the default (already installed open-jdk) java version. How can I override the default open-jdk with the one I downloaded?
UPDATE:
which java says /usr/bin/java
$JAVA_HOME/bin/java -version says 'Permission Denied'
sudo $JAVA_HOME/bin/java -version (asks for password, then) says Command not found
but cd $JAVA_HOME/bin, and ls shows that it is right directory.
While it looks like your setup is correct, there are a few things to check:
The output of env - specifically PATH.
command -v java tells you what?
Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.
I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?
Also, you can help yourself in future maintenance of your JDK installation by writing this instead:
export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=$JAVA_HOME/bin:$PATH
Then you only need to update one env variable when you setup the JDK installation.
Finally, you may need to run hash -r to clear the Bash program cache. Other shells may need a similar command.
Cheers,
update-java-alternatives
The java executable is not found with your JAVA_HOME, it only depends on your PATH.
update-java-alternatives is a good way to manage it for the entire system is through:
update-java-alternatives -l
Sample output:
java-7-oracle 1 /usr/lib/jvm/java-7-oracle
java-8-oracle 2 /usr/lib/jvm/java-8-oracle
Choose one of the alternatives:
sudo update-java-alternatives -s java-7-oracle
Like update-alternatives, it works through symlink management. The advantage is that is manages symlinks to all the Java utilities at once: javac, java, javap, etc.
I am yet to see a JAVA_HOME effect on the JDK. So far, I have only seen it used in third-party tools, e.g. Maven.
If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:
I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
I ran:
sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
Now you can execute sudo update-alternatives --config java and choose your java version.
This doesn't set the JAVA_HOME variable, which I wanted configured, so I just added it to my ~/.bashrc, including an export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35" statement
Now, I had two JDKs downloaded (let's say the second has been extracted to /home/aqeel/development/jdk/jdk-10.0.1).
How can we change the JAVA_HOME dynamically based on the current java being used?
My solution is not very elegant, I'm pretty sure there are better options out there, but anyway:
To change the JAVA_HOME dynamically based on the chosen java alternative, I added this snippet to the ~/.bashrc:
export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print $2}' | awk -F'/bin/java' '{print $1}')
Finally (this is out of the scope) if you have to change the java version constantly, you might want to consider:
Adding an alias to your ~./bash_aliases:
alias change-java="sudo update-alternatives --config java"
(You might have to create the file and maybe uncomment the section related to this in ~/.bashrc)
$JAVA_HOME/bin/java -version says 'Permission Denied'
If you cannot access or run code, it which be ignored if added to your path. You need to make it accessible and runnable or get a copy of your own.
Do an
ls -ld $JAVA_HOME $JAVA_HOME/bin $JAVA_HOME/bin/java
to see why you cannot access or run this program,.
When it searches for java it looks from left to right in path entries which are separated by : so you need to add the path of latest jdk/bin directory before /usr/bin, so when it searches it'll find the latest one and stop searching further.
i.e. PATH=/usr/java/jdk_1.8/bin:/usr/bin:..... and so on.
then initialize user profile using command: source ~/.bash_profile
and check with: [which java]
you'll get the right one.
check available Java versions on your Linux system by using update-alternatives command:
$ sudo update-alternatives --display java
Now that there are suitable candidates to change to, you can switch the default Java version among available Java JREs by running the following command:
$ sudo update-alternatives --config java
When prompted, select the Java version you would like to use.1 or 2 or 3 or etc..
Now you can verify the default Java version changed as follows.
$ java -version
Try this:
export JAVA_HOME=put_here_your_java_home_path
type export PATH=$JAVA_HOME/bin:$PATH (ensure that $JAVA_HOME is the first element in PATH)
try java -version
Reason: there could be other PATH elements point to alternative java home. If you put first your preferred JAVA_HOME, the system will use this one.
There is an easy way, just remove the symbolic link from "/usr/bin". It will work.
Updating the ~/.profile or ~/.bash_profile does not work sometimes. I just deleted JDK 6 and sourced .bash_profile.
Try running this:
sudo rm -rd jdk1.6.0_* #it may not let you delete without sudo
Then, modify/add your JAVA_HOME and PATH variables.
source ~/.bash_profile #assuming you've updated $JAVA_HOME and $PATH
In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.sh I renamed this file to jdk_home.sh.old and now my path does not keep getting overridden and I can call java -version and see Java 9 as expected. Even though I correctly selected Java 9 in update-aternatives --config java this jdk_home.sh file kept overriding the $PATH on boot-up.

Categories