How to switch the linux jdk version at any time

Source: Internet
Author: User
Tags versions

Project handling issues encountered in the following cases, referencing a third party jar package reported classdefnotfoundexception error caused the app crash, the two sides communicate to confirm the existence of the class and eliminate the project is not clean, code confusion caused by, and so on.

Notice that when you compile the app, there are a lot of things that are different than normal:

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 exactly the class classdefnotfoundexception the error.

Baidu Search appears this compilation hints to find the cause of the problem: third-party companies to pack this jar package using the jdk1.7 version (greater than 1.6), And we compile the app using version 1.6, because the compiled bytecode version is inconsistent, resulting in the inability to convert to the Android virtual machine scale byte code, so the reference jar package is not actually compiled into the current APK, the program is running, of course, can not find the corresponding class.

The corporate build server is actually configured with multiple JDK versions, and Android compilation is the JDK version set when setting environment variables. Compilation android4.4 uses jdk1.6, and compilation android5.0 uses jdk1.7, which involves switching JDK versions at any time.

First to see how to set the JDK environment variables, Baidu Search to get the answer is to modify ~/.BASHRC or modify the/etc/profile file one-time completion configuration. I use VI to open these two files do not find the corresponding JDK environment variables, but you can see the default JDK version using Java-version. This raises the question of where the current default JDK software's environment variables are configured?

Find the location of the current Java executable using the which Java command

fordreamxin@compiler207:~$ which Java

/usr/bin/java

The/usr/bin/directory is stored in more than the user-installed software

fordreamxin@compiler207:~$ Ll/usr/bin/java

lrwxrwxrwx 1 root Nov 06:18/usr/bin/java->/etc/alternatives/java*

Alternatives is a multiple version management software under Linux, which can be used to achieve version switching of JDK, which is described later. First come to the alternatives directory to see what's on the ground.

fordreamxin@compiler207:/etc/alternatives$ 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

fordreamxin@compiler207:/etc/alternatives$ ll Java

lrwxrwxrwx 1 root Nov 06:17 java->/usr/lib/jvm/java-6-sun/jre/bin/java*

There was a lot of software version references in the directory, from where you can find the actual location of the current software version (JDK)

fordreamxin@compiler207:/etc/alternatives$ ls/usr/lib/jvm/

Java-1.7.0-openjdk-amd64 Java-6-sun java-6-sun-1.6.0.26 Java-7-openjdk-amd64

Under the/USR/LIB/JVM folder, the software for each JDK version is stored, and alternatives is the purpose of the version switch by modifying its Java references.

The way to implement version switching using alternatives is as follows:

1. See if the corresponding JDK is in the Ubuntu JDK menu to view:

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: Changes in the Jdk1.6.0_12 version will vary

3.sudo Update-alternatives--config Java

sudo update-alternatives--config javac

Select the 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 well as modifying ~/.BASHRC or/etc/profile, one operation, globally valid. What if you don't have super privileges?

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

fordreamxin@compiler207:/$ Echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

The above command shows the current environment variable, which is the content of the ~/.BASHRC or/etc/profile file, which represents all the current environment variables for all software or commands that can be used globally, without these variables, we can't use many commands or software in any directory. This is also the meaning of environmental variables.

Use the following command to add JDK environment variables at the beginning or end of the $path (save the executable commands for Java, Javac, Javah, etc. in the bin directory)

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

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

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

The specific use of the

Export can be referred to its manual.

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.