Linux JDK version ready to switch

Source: Internet
Author: User
Tags java reference

Project handling issues encountered in the following cases, referring to the third party jar package reported classdefnotfoundexception error caused the app crashes, the two sides communicate to confirm that the class exists and exclude the project is not clean, code confusion caused by the reason.

Note that when compiling the app, there are a number of messages that are different from the normal circumstances:

Trouble processing:
Bad class file Magic (Cafebabe) or version (0033.0000)
... while parsing com/baidu/mapapi/sdkinitializer.class
... while processing Com/baidu/mapapi/sdkinitializer.class
279 Warnings Sdkinitializer is the class of classdefnotfoundexception error. Baidu Search This compilation hint found the cause of the problem: the third-party company packaged the jdk1.7 version (greater than 1.6) used by the jar, and we compiled the app using version 1.6, because the compiled bytecode version is inconsistent, resulting in the inability to convert the bytecode to the Android virtual machine scale, so the referenced jar package Actually not compiled into the current APK, when the program runs, of course, can not find the corresponding class. The company compiled server is actually configured with multiple JDK versions, and Android compilation is the JDK version set when setting environment variables. Compiling android4.4 uses jdk1.6, and compiling android5.0 uses jdk1.7, which involves switching the JDK version at any time. First to see how to set the JDK environment variables, Baidu Search to get the answer is to modify the ~/.BASHRC or modify the/etc/profile file to complete the configuration once. I use VI to open these two files do not find the corresponding JDK environment variables, but using java-version do see the default JDK version. Here's the question, where is the current default JDK environment variable configured? Find the location of the current Java executable using the which Java command
which Java/usr/bin/java

User-installed software is stored in the/usr/bin/directory

[Email protected]:~$ ll/usr/bin/1:/usr/bin/java- >/etc/alternatives/java*

Alternatives is a multi-version management software under Linux that can be used to implement version switching of the JDK, which is described later. Let's go to the alternatives catalogue and see what's on the ground.

ls java*java  java. 1. gz  javac  javac.  1. gz  Javadoc  Javadoc.  1. gz  javah  javah.  1. gz  javap  javap.  1. gz  java_vm  javaws  javaws.  1. gz
[Email protected]:/etc/1: + java- /usr/lib/jvm/ java-6-sun/jre/bin/java*

It turns out that there are many versions of the software in this directory, where you can find the actual location of the current software version (JDK).

ls /usr/lib/jvm/java-1.7. 0-openjdk-amd64  java-6-sun  java-6-sun-1.6.  0.26  java-7-openjdk-amd64

In the/USR/LIB/JVM folder the time is stored for each JDK version of the software, and alternatives is to modify its Java reference to achieve version switching.
There are several ways to implement version switching using alternatives :

1. Check to see if the JDK is in the Ubuntu JDK menu:
Update-alternatives--config Java
Update-alternatives--config Javac

2. If not in the menu can be added as follows:
Update-alternatives--install/usr/bin/java Java/usr/lib/jvm/java/jdk1.6.0_12/bin/java 300
Update-alternatives--install/usr/bin/javac Javac/usr/lib/jvm/java/jdk1.6.0_12/bin/javac 300
Note: Different versions of Jdk1.6.0_12 are subject to change

3.sudo Update-alternatives--config Java
sudo update-alternatives--config javac
Select serial number, enter can;
4. Then java-version,javac-version view the current JDK version

However, using alternatives requires sudo super-privilege, which means that this switch is a global switch, as with the modification of ~/.BASHRC or/etc/profile, one operation is globally valid. What if I don't have super privileges?

Setting environment variables Linux provides the Export command, which is only valid for the current terminal .

Echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

The above command shows the current environment variable, that is, the contents of the ~/.BASHRC or/etc/profile file, which represents all the current environment variables of any software or command that can be used globally, and without these variables, we cannot use many commands or software in any directory. This is also the meaning of environmental variables.
You can add the JDK environment variable at the beginning or end of the $path by using the following command (Java, Javac, Javah and other executable commands are saved in the bin directory)

Export path="/usr/lib/jvm/java-7-openjdk-amd64/bin": $PATHexport PATH="  $PATH:/usr/lib/jvm/java-7-openjdk-amd64/bin/"

Since the execution always searches for the location of the executable from the beginning of $path, if a JDK is already set up in the environment variable, it will be useful to set the new JDK at the beginning of the path.

The specific use of export may refer to its manual.

Linux JDK version switches at any time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.