带有字符串的开关在 Java 7 中无法编译



我有一个项目,用于编译和运行良好。然而,当我尝试使用带有字符串的switch和java7编译器时,它会抱怨。我使用Ant构建了这个项目。

Ant配置文件(projectBuilder.xml):

<?xml version="1.0"?>
<project name="TutoMigLayout" default="Main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />
  <property name="lib.dir" location="lib" />
  <property name="build.dir" location="bin" />
  <!--
    Create a classpath container which can be later used in the ant task
  -->
  <path id="build.classpath">
    <fileset dir="${lib.dir}">
      <include name="**/*.jar" />
    </fileset>
  </path>
  <!-- Deletes the existing build directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
  </target>
  <!-- Creates the  build  directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
  </target>
  <!-- Compiles the java code -->
  <target name="compile" depends="clean, makedir">
  <echo message="Using Java version ${ant.java.version}."/>
    <javac target="1.7" srcdir="${src.dir}" destdir="${build.dir}" debug="true" classpathref="build.classpath" />
  </target>
  <target name="Main" depends="compile">
    <description>Main target</description>
  </target>
</project> 

我在控制台上看到这个:

Buildfile: C:UsersworkspaceTutoMigLayoutprojectBuilder.xml
clean:
[delete] Deleting directory C:UsersworkspaceTutoMigLayoutbin
makedir:
[mkdir] Created dir: C:UsersworkspaceTutoMigLayoutbin
compile:
[echo] Using Java version 1.7.
[javac] C:UsersworkspaceTutoMigLayoutprojectBuilder.xml:31: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 5 source files to C:UsersworkspaceTutoMigLayoutbin
[javac] javac: invalid target release: 1.7
[javac] Usage: javac <options> <source files>
[javac] use -help for a list of possible options
BUILD FAILED
C:UsersworkspaceTutoMigLayoutprojectBuilder.xml:31: Compile failed; see the compiler error output for details.
Total time: 481 milliseconds

当我从编译目标中删除编译器版本时,我得到的是:

[javac] C:Usersinvite01.acensiworkspaceTutoMigLayoutsrccomponentsEqualsAction.java:26: incompatible types
[javac] found   : java.lang.String
[javac] required: int
[javac]         switch(operator) {
[javac]                ^

我以为我们可以在Java7中使用Strings进行切换?

如果我再点击一次run,我会得到一条完全不同的消息:

Error : could not find or load the main class test.Test

编辑:javac-版本javac 1.6.0_32好的,我该怎么改?我认为将编译器遵从性级别设置为1.7并选择jre7就足够了?

尝试添加source="1.7"属性。这控制了语言功能。但是,在大多数情况下,targetsource属性都应该设置为相同的版本。而且,正如其他人所提到的,编译器必须支持这个版本。

相关内容

  • 没有找到相关文章

最新更新