使用字符串切换不能在Java 7中编译

时间:2013-08-28 15:04:54

标签: ant java-7

我有一个用于编译和运行的项目。但是,当我尝试使用带有Java 7编译器的字符串开关时,它会抱怨。我使用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:\Users\workspace\TutoMigLayout\projectBuilder.xml
clean:
[delete] Deleting directory C:\Users\workspace\TutoMigLayout\bin
makedir:
[mkdir] Created dir: C:\Users\workspace\TutoMigLayout\bin
compile:

[echo] Using Java version 1.7.
[javac] C:\Users\workspace\TutoMigLayout\projectBuilder.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:\Users\workspace\TutoMigLayout\bin
[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:\Users\workspace\TutoMigLayout\projectBuilder.xml:31: Compile failed; see the compiler error output for details.

Total time: 481 milliseconds

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

[javac] C:\Users\invite01.acensi\workspace\TutoMigLayout\src\components\EqualsAction.java:26: incompatible types
[javac] found   : java.lang.String
[javac] required: int
[javac]         switch(operator) {
[javac]                ^

我以为我们可以在Java 7中使用字符串切换?

如果我再次点击运行,我会收到完全不同的消息:

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

编辑:javac -version javac 1.6.0_32 好的我怎么改变它?我认为将编译器合规性级别设置为1.7并且选择jre7就够了吗?

1 个答案:

答案 0 :(得分:2)

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