Linux JDK version switch at any time


The following situations are encountered in project processing. The third-party jar package is referenced and classdefnotfoundexception error is reported, which leads to app crash. Both parties communicate and confirm that the class exists and exclude the reasons such as project is not clean, code confusion, etc.

Note that when compiling an app, you will be prompted with a lot of information that 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 an error.
Baidu search found the reason for the problem: the third-party company used jdk1.7 (greater than 1.6) to package the jar package, while we used version 1.6 to compile the app. Due to the inconsistent version of the bytecode compiled, the bytecode could not be converted into the bytecode of Android virtual machine scale, so the referenced jar package was not actually compiled into the current APK, the program Of course, no corresponding class can be found when 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 Android 4.4 uses JDK1.6, while compiling Android 5.0 uses jdk1.7, which involves how to switch the JDK version at any time.
First, let’s see how to set the JDK environment variable. Baidu search gets the answer mostly 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 I can see the default JDK version with java version. This raises the question, where is the current environment variable of the default JDK software configured?
Use the which Java command to find the location of the current Java executable
[email protected]:~$ which java
Most of the software stored in / usr / bin / directory is user installed software

[email protected]:~$ 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. It can be used to switch JDK versions, which will be described later. Let’s take a look at the alternatives directory first.

[email protected]:/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
[email protected]:/etc/alternatives$ ll java
lrwxrwxrwx 1 root root 36 Nov 13 06:17 java -> /usr/lib/jvm/java-6-sun/jre/bin/java*

Many version references of software were placed in the directory, from which you can find the actual location of the current software version (JDK)

[email protected]:/etc/alternatives$ ls /usr/lib/jvm/
java-1.7.0-openjdk-amd64  java-6-sun  java-6-sun-  java-7-openjdk-amd64
In the / usr / lib / JVM folder, the software of each JDK version is stored in time, and alternates achieves the purpose of version switching by modifying its java reference.
The methods of version switching using alternatives are as follows:

1. Check whether the corresponding JDK is in the JDK menu of Ubuntu, and check:
update-alternatives –config java
update-alternatives –config javac

2. If it is not in the menu, it 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 will change

3.sudo update-alternatives –config java
sudo update-alternatives –config javac
Select the serial number and enter;
4. Then java version and javac version view the current JDK version


But using alternates requires sudo super permission, that is to say, this kind of switch is a global switch. Like modifying ~ /. Bashrc or / etc / profile, one operation is globally effective. What if you don’t have super permission?

Setting the environment variable Linux provides the export command, which is only valid for the current terminal.

[email protected]:/$ echo $PATH
The above command shows the current environment variables, that is, the contents of ~ /. Bashrc or / etc / profile file. It represents all the current software or command environment variables that can be used globally. Without these variables, we can’t use many commands or software in any directory, which is also the meaning of environment variables.
You can add JDK environment variables at the beginning or end of $path through the following commands (Java, javac, javah and other executable commands are saved in 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 the location of the executable is always searched from the beginning of $path, it is only useful to set a new JDK at the beginning of the path if a JDK has been set in the environment variable.

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

Recommended Today

(73) C × WinForm custom control – resource loading form – hzhcontrols

Official website premise It’s been 7 or 8 years since I started, I always want to make a set of beautiful custom controls, so I have this series of articles. GitHub: Code cloud: WinForm custom control.git If you think it’s OK, please click star to support it Welcome to discuss: Penguins 568015492 Please […]