1

我有一个用于编译和运行良好的项目。但是,当我尝试使用带有 java 7 编译器的 Strings 开关时,它会抱怨。我使用 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 就足够了?

4

1 回答 1

2

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

于 2013-08-28T15:16:25.757 回答