l0r3nz4cc10
l0r3nz4cc10

Reputation: 1283

switch with Strings doesn't compile in Java 7

I have a project which used to compile and run fine. However, it complains when I try to use switch with Strings, with a java 7 compiler. I build this project using Ant.

The Ant config file (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> 

I get this in the console :

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

When I remove the compiler version from the compile target, I get that instead :

[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]                ^

I thought we could switch with Strings in Java 7 ?

If I click run one more time, I get a completely different message :

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

Edit : javac -version javac 1.6.0_32 OK how do I change that ? I thought setting compiler compliance level to 1.7 and selecting jre7 was enough ?

Upvotes: 1

Views: 890

Answers (1)

Holger
Holger

Reputation: 298233

Try adding a source="1.7" attribute. This controls the language features. However, in most cases both target and source attributes should be set to the same version. And, as mentioned by others, the compiler must support this version.

Upvotes: 2

Related Questions