[英]Enabling Java assertions not working with Eclipse Luna
I tried enabling assertions for Java 8 in Eclipse Luna by doing what most answers on here suggest, adding -ea as a VM argument in the Run config, as well as in the default VM arguments in the JRE config. 我尝试通过执行此处大多数答案所建议的操作来在Eclipse Luna中启用Java 8的断言,在运行配置中以及-JRE配置中的默认VM参数中添加-ea作为VM参数。 Still I get the same error as before:
我仍然收到与以前相同的错误:
Multiple markers at this line - 'assert' should not be used as an identifier, since it is a reserved keyword from source level 1.4 on - Syntax error on token "assert", ( expected - Syntax error, insert "AssignmentOperator Expression" to complete Expression - Syntax error, insert ")" to complete Expression
此行上的多个标记-不应将'assert'用作标识符,因为它是源级别1.4上的保留关键字-令牌“ assert”上的语法错误,(预期-语法错误,请插入“ AssignmentOperator Expression”以完成表达式-语法错误,插入“)”以完成表达式
My program is really just a simple program to try it out: 我的程序实际上只是一个可以尝试的简单程序:
public class Test {
public static void main (String[] args) {
int x = 10;
assert x != 0 : "Test";
System.out.println(x);
}
}
What am I doing wrong? 我究竟做错了什么?
将括号括在x!= 0周围。这可能会解决您的问题。
Go to Window
-> Preferences
-> Java
-> Installed JREs
-> select your installed JVM -> Edit..
-> in "Default VM arguments:" add -ea
. 转到
Window
- > Preferences
- > Java
- > Installed JREs
- >选择您所安装的JVM - > Edit..
- >在“默认VM参数:”加-ea
。
Finally make sure that your project uses same JVM in which you added -ea
as default argument. 最后,确保您的项目使用添加
-ea
作为默认参数的相同JVM。 To do that : 要做到这一点 :
right click on project
-> Properties
-> JavaBuildPath
-> Libraries
here you should see JRE system library[...]
. right click on project
-> Properties
-> JavaBuildPath
> Libraries
在这里您应该看到JRE system library[...]
。 Make sure that in [...]
is JVM you edited to enable assertions. 确保在
[...]
是JVM您编辑启用断言。
I thought I anwer my own question, since the solution was hidden in a comment until now: 我以为自己提出了自己的问题,因为该解决方案直到现在都被隐藏在注释中:
Removing -ea from the default parameters, but keeping it in the run configuration solved the problem, for me. 从我的默认参数中删除-ea,但是将其保留在运行配置中可以解决此问题。
在Eclipse中,右键单击Project-> properties->选择Java编译器->单击Java构建路径->选择jre->单击edit-> Execution Environment选择J2SE-1.5(jre1.8.0_65)->完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.