jdk switch version

The project processing problem encountered the following situations. The ClassDefNotFoundException error was reported by referencing a third-party jar package, causing the app to crash. The two parties communicated to confirm the existence of the class and ruled out that the project was not cleaned, and the code was confused.

I noticed that when compiling the app, a lot of the following information is different from the normal situation:

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 that ClassDefNotFoundException reports.

 

Baidu search showed this compilation prompt and found the cause of the problem: the third-party company used jdk1.7 version (greater than 1.6) to package this jar package, and we compiled the app using version 1.6, because the compiled bytecode version is inconsistent , which cannot be converted into the bytecode of the android virtual machine scale, so the referenced jar package is not actually compiled into the current apk, and of course the corresponding class cannot be found when the program is running.

 

The company's compilation 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, while compiling android5.0 uses jdk1.7, which involves how to switch jdk versions at any time.

 

First, let's take a look at how to set the jdk environment variable. Most of the answers obtained by Baidu search are to modify ~/.bashrc or modify the /etc/profile file to complete the configuration at one time. I opened these two files with vi and did not find the corresponding jdk environment variables, but using java -version did see the default jdk version. This is a question, where are the environment variables of the current default jdk software configured?

 

Use the which java command to find the location of the current java executable

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

 Most of the software installed by the user is stored in the /usr/bin/ directory

fordreamxin@compiler207:~$ ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Nov 13 06:18 /usr/bin/java -> /etc/alternatives/java*

alternatives is a multi-version management software under linux, which can be used to implement jdk version switching, which will be described later. First go to the alternatives directory to see what is there.

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 root 36 Nov 13 06:17 java -> /usr/lib/jvm/java-6-sun/jre/bin/java*

It turns out that a lot of software version references are placed in this directory, from which 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

The software of each jdk version is stored in the /usr/lib/jvm folder, and the alternatives achieve the purpose of version switching by modifying its java reference.
The method of using alternatives to implement version switching is as follows :

1. Check whether the corresponding jdk is in the jdk menu of ubuntu, check:
update-alternatives --config java
update-alternatives --config javac

2. If it is not in the menu, you can add it 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: jdk1.6.0_12 version will change

3. sudo update -alternatives --config java
sudo update-alternatives --config javac
Select the serial number and press Enter;
4. Then java -version, javac -version view the current jdk version

 

However, the use of alternatives requires sudo super permission, which means that this switch is a global switch. It is the same as modifying ~/.bashrc or /etc/profile. One operation is globally effective. What if I don't have super permissions?

Setting the environment variable linux provides the export command, and the modification of this command 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 displays the current environment variables, that is, the content of the ~/.bashrc or /etc/profile file, which represents the current environment variables of all software or commands that can be used globally. Many commands or software are used in the directory, which is also the meaning of environment variables.
The jdk environment variable can be added at the beginning or end of $PATH by the following command (the bin directory saves executable commands such as java, javac, javah, etc.)

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

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

Since the execution always searches for the location of the executable file from the beginning of the $PATH, if a jdk is already set in the environment variable, setting the new jdk at the beginning of the PATH will only be useful.

For the specific use of export, please refer to its manual.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877394&siteId=291194637