我最近更新了我的GWT项目到2.6.1,并开始使用Java 7语法,因为2.6现在支持Java 7。
然而,当我尝试编译时,我收到编译器错误,如
[ERROR] Line 42: '<>' operator is not allowed for source level below 1.7
全部输出
Compiling module com.Project
Validating units:
Ignored 85 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Computing all possible rebind results for 'com.google.gwt.useragent.client.UserAgentAsserter'
Rebinding com.google.gwt.useragent.client.UserAgentAsserter
Checking rule <generate-with class='com.google.gwt.editor.rebind.SimpleBeanEditorDriverGenerator'/>
[WARN] Detected warnings related to 'com.google.gwt.editor.client.SimpleBeanEditorDriver'. Are validation-api-<version>.jar and validation-api-<version>-sources.jar on the classpath?
Specify -logLevel DEBUG to see all errors.
[WARN] Unknown type 'com.google.gwt.editor.client.SimpleBeanEditorDriver' specified in deferred binding rule
[ERROR] Errors in 'file:/C:/workspace/Project/src/com/project/ProjectFile.java'
[ERROR] Line 42: '<>' operator is not allowed for source level below 1.7
[ERROR] Errors in 'file:/C:/workspace/Project/src/com/project/AnotherProjectFile.java'
[ERROR] Line 188: Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
C:workspaceProjectbuild.xml:51: Java returned: 1
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.Main.start(Main.java:180)
at org.apache.tools.ant.Main.main(Main.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
C:workspaceProjectbuild.xml:51: Java returned: 1
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.Main.start(Main.java:180)
at org.apache.tools.ant.Main.main(Main.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
C:workspaceProjectbuild.xml:51: Java returned: 1
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:111)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.Main.start(Main.java:180)
at org.apache.tools.ant.Main.main(Main.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:30)
如何将GWT编译器指定为目标1.7?我以为它会默认这样做,但我想不是。我试图清理项目,包括删除gwt-unitCache目录,但无济于事。
这是我的Ant编译目标。
<target name="compile" depends="prepare">
<javac includeantruntime="false"
debug="on"
debuglevel="lines,vars,source"
srcdir="${src.dir}"
destdir="${build.dir}"
source="7"
target="7">
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="gwt-compile" depends="compile">
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<!-- src dir is added to ensure the module.xml file(s) are on the classpath -->
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
<path refid="project.classpath"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-sourceLevel"/><arg value="7"/>
<arg value="${gwt.module.name}"/>
</java>
</target>
GWT编译器(和SuperDevMode的CodeServer)有一个-sourceLevel
参数,您可以使用它来指定语言级别,类似于javac
的-source
参数。
-sourceLevel
的默认值取决于当前的JVM,所以如果使用JDK 1.7,它应该默认为Java 7。
明确地将-sourceLevel 7
(或1.7
)传递给GWT,将-source 7
(或1.7
)传递给javac
(在<javac>
Ant任务中使用source="7"
)是一个很好的做法
我们刚刚花了两天时间来寻找一个类似的bug,结果发现这是由于使用错误的java版本(1.6)编译gwt代码造成的。这在gwt-unitCache
中留下了一些数据,这在某种程度上迫使gwt编译器保持期望代码为1.6(因此菱形操作符警告),即使java版本更改为1.7。问题已通过删除gwt-unitCache
解决。
我知道OP说他已经尝试过了,但并没有解决他的问题,但我想提一下,因为它可能有助于其他有类似问题的人。此外,这也可能表明,如果OP使用其他预编译的gwt代码,这可能会影响gwt编译器使用的java版本。
最终,解决方案是zpon和Thomas Broyer的答案的结合。
首先,删除gwt-unitCache
目录。
然后,在javac
Ant任务中,显式指定源级别
<javac includeantruntime="false"
debug="on"
debuglevel="lines,vars,source"
srcdir="${src.dir}"
destdir="${build.dir}"
source="7"
target="7">
<classpath refid="project.classpath"/>
</javac>
还要指定GWT编译器的源级别
<java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">
<classpath>
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
<path refid="project.classpath"/>
</classpath>
<jvmarg value="-Xmx256M"/>
<arg value="-sourceLevel"/><arg value="7"/>
<arg value="${gwt.module.name}"/>
</java>