尝试使用maven从命令行运行Java7 Hello World Project
尝试使用maven从命令行运行Java7 Hello World Project。如果我从eclipse中运行代码,我的项目运行正常,但如果我尝试执行“mvn packege”包,我会收到以下错误..
这是我的来源:
public class App { public static void main( String[] args ) { System.out.println( "Hello World!, Tesing Java 7" ); printDay("Wednesday"); } public static void printDay(String dayOfWeek) { switch (dayOfWeek) { case "Sunday": System.out.println("Dimanche"); break; case "Monday": System.out.println("Lundi"); break; case "Tuesday": System.out.println("Mardi"); break; case "Wednesday": System.out.println("Mercredi"); break; case "Thursday": System.out.println("Jeudi"); break; case "Friday": System.out.println("Vendredi"); break; case "Saturday": System.out.println("Samedi"); break; default: System.out.println("Error: '"+ dayOfWeek +"' is not a day of the week"); break; } } }
这是我的pom.xml文件:
4.0.0 org.smith TestJava7 0.0.1-SNAPSHOT jar TestJava7 http://maven.apache.org UTF-8 junit junit 3.8.1 test
这是错误:
[ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /home/jsmith/Documents/workspace-juno/TestJava7/src/main/java/org/smith/TestJava7/App.java:[16,13] error: strings in switch are not supported in -source 1.5 [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.169s [INFO] Finished at: Wed Jan 16 09:48:26 EST 2013 [INFO] Final Memory: 10M/100M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project TestJava7: Compilation failure [ERROR] /home/jsmith/Documents/workspace-juno/TestJava7/src/main/java/org/smith/TestJava7/App.java:[16,13] error: strings in switch are not supported in -source 1.5 [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
设置comiler插件的源和目标版本:
http://maven.apache.org/plugins/maven-compiler-plugin/
[...] [...] org.apache.maven.plugins maven-compiler-plugin 3.0 1.7 1.7 [...] [...]
错误显然是在说:
strings in switch are not supported in -source 1.5
您需要将maven-compiler-plugin
配置为指向JDK 7,默认情况下它将指向1.5或1.6
请尝试此处以获取有关如何配置相同内容的更多信息 。
将maven插件添加到POM.xml中,清理并再次运行..
org.apache.maven.plugins maven-compiler-plugin 3.0 1.7 1.7