错误:钻石操作符在-source1.5中不受支持。

6
我正在创建一个应用程序,它使用cordova ionic和angular,并且对于条形码扫描,我正在使用本机,并能够与javascript代码集成。如果我使用eclipse IDE运行项目,它可以正常工作,但是如果我使用ionic run android运行,则会出现上述错误-钻石操作符在源1.5中不受支持。
对于本机,我使用了这个链接https://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/,并且它可以正常工作。
有人可以帮忙解决这个问题吗?

您正在使用<>,但您所使用的Java源代码不支持它,因为它只在Java 1.7中添加了该功能。 - Ceiling Gecko
使用类元素对菱形<>进行参数化,这些元素必须包含。 - Jordi Castilla
$ java -version java版本 "1.8.0_25" Java(TM) SE Runtime Environment (构建 1.8.0_25-b18) Java HotSpot(TM) 64位服务器虚拟机 (构建 25.25-b02, 混合模式) - sudarshan
@CeilingGecko 如果我使用IDE运行,它会毫无错误地运行。:( - sudarshan
@sudarshan 可能情况是,IDE 编译的 Java 源代码与 Android 不同。 - Ceiling Gecko
4个回答

11
尝试按照 @Sudarshan 的建议,在 Build 标签下将 apache 插件添加到 Pom.xml 中。
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
  </plugins>
</build>

这将解决问题。


5

你正在使用的<>在你所使用的Java源代码中不受支持,因为它只在Java 1.7中才被添加。

查找你源代码中使用<>的位置,并正确指定所隐含的泛型。

例如,如果是以下内容:

List<String> myList = new ArrayList<>();

将其重写为:

List<String> myList = new ArrayList<String>();

注意:虽然钻石操作符是一个便捷的快捷方式,但我建议始终指定完整的泛型,因为它不仅增加了可读性,而且不会在您的源代码中创建1.7+依赖关系。 (正如我们所看到的,有时会导致问题。)


有没有办法更新项目的Java版本,因为在很多地方都需要更改。 - sudarshan
如果你正在使用 Cordova,你可以查看这个问题的答案。 @sudarshan - Ceiling Gecko
@sudarshan 是的,如果你想让Maven使用其他东西,你必须进行配置 - Jesper
<properties> <sourceJdk>1.7</sourceJdk> <targetJdk>1.7</targetJdk> </properties> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> 我把这些行粘贴到pom.xml中,但没有成功。 - sudarshan
@CeilingGecko 在 ant 文件夹中更改了工具目录中的 Java 目标版本后,现在在运行 ionic run android 命令时出现了“找不到符号”错误。 - sudarshan
@sudarshan,你能在原始帖子中添加新错误的详细描述(例如堆栈跟踪)吗? - Ceiling Gecko

0

右键单击项目 -> 属性 -> 项目面板 -> 勾选Java,选择1.7


0
+1 给 @Swarit Agarwal 的回答,以下是 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>order-rest-marvin</groupId>
  <artifactId>order-rest-marvin</artifactId>
  <version>0.0.1-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
    </parent>

  <dependencies>
    <dependency> 
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
  </dependencies>

  <!-- to resolve the issue about maven plugin not supported (older versions) -->
  <build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
  </plugins>
</build>

</project>

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接