我遇到了一个问题,当我尝试在我的代码中使用String.format
时,Maven编译过程似乎会抛出错误。
我正在使用Maven 3.0.4和Java 1.7.0_25。我创建了一个这样的测试项目:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -groupId=com.mycompany.app -DartifactId=testapp
然后我将位于src/main/java/com/mycompany/app/App.java
的主要方法更改为:
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( String.format("Hello World!") );
}
}
请注意String.format
来电中的System.out.println
。
在执行mvn compile
时,我现在收到以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project testapp: Compilation failure
[ERROR] /home/me/projects/java/testapp/src/main/java/com/mycompany/app/App.java:[11,34] error: cannot find symbol
[ERROR] -> [Help 1]
我不知道如何解决这个问题。为了完整性,这是我生成的pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>testapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
更新
Maven版本信息:
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_AU, platform encoding: UTF-8
OS name: "linux", version: "3.5.0-37-generic", arch: "amd64", family: "unix"
答案 0 :(得分:3)
尝试将其添加到pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
我认为默认情况下,maven会尝试编译您的代码以与1.4兼容。
查看此链接以获取更多信息: http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html