Ubuntu 16.04执行mvn命令报错:环境无编译器,求解决方案
Hey there, let's work through this Maven compiler error together. Since you've already checked Eclipse's JRE settings and tried common fixes, let's focus on Ubuntu-specific system-level and Maven configuration tweaks that might resolve this issue.
1. Verify System-Wide JDK Installation & Configuration
First, let's make sure your terminal environment is actually using a JDK (not just a JRE)—this is often the hidden culprit when Eclipse works but terminal Maven commands fail.
Check your current Java environment:
Run these two commands in your terminal to confirm:java -version javac -versionIf
javacthrows an error, your system doesn't have a JDK properly configured at the terminal level.Install the reliable OpenJDK for Ubuntu 16.04:
sudo apt-get update sudo apt-get install openjdk-8-jdkSet the default JDK for your system:
Run these commands and select the JDK option (not JRE) from the displayed list:sudo update-alternatives --config java sudo update-alternatives --config javac
2. Configure the JAVA_HOME Environment Variable
Maven relies heavily on the JAVA_HOME variable to locate the JDK. Let's ensure it's pointing to the correct path:
Open your bash profile for editing:
nano ~/.bashrcIf you use a different shell (like zsh), edit
~/.profileinstead.Add these lines at the end of the file (replace the path with your actual JDK install path—you can get this from the
update-alternativescommand output):export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATHSave the file and apply the changes immediately:
source ~/.bashrcVerify the variable is set correctly:
echo $JAVA_HOME javac -version
3. Force Maven to Use the Correct Compiler via pom.xml
If system-level changes still don't work, you can explicitly tell Maven which compiler to use in your project's pom.xml:
Add this build plugin configuration inside your <project> tag:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <fork>true</fork> <executable>/usr/lib/jvm/java-8-openjdk-amd64/bin/javac</executable> </configuration> </plugin> </plugins> </build>
This forces Maven to use the specified JDK compiler, bypassing any system environment mismatches.
4. Double-Check Terminal vs. Eclipse Environment
Remember: Eclipse's internal Java settings are separate from your terminal's environment. Even if Eclipse is configured correctly, your terminal might still be using a JRE. After setting up JAVA_HOME and the default JDK, close and reopen your terminal to ensure the changes take effect, then run your Maven command again.
内容的提问来源于stack exchange,提问作者Bhargav

