Java assertions
OK, I need to put assertions into my Java code, but I can't figure out how to enable assertions in my NetBeans 6.8 IDE.
Is this the only step I need to complete in order to be able to use assertions?
Just to set this up, I am creating simple Java code:
class Main
public static void main(String[] 开发者_JS百科args) throws IOException {
System.out.print("Enter your marital status: ");
int c = System.in.read();
switch ((char) c) {
case 's':
case 'S': System.out.println("Single"); break;
case 'm':
case 'M': System.out.println("Married"); break;
case 'd':
case 'D': System.out.println("Divorced"); break;
default: assert !true : "Invalid Option"; break;
}
}
}
Then I go to Run* → Set project configuration → Customize...
I enter in the "VM Options" box: -enableassertions:javaapplication58
As javaapplication58 is my package
I have used this approach because upon googling a bit, it looks like what you do, but I still can't get my assert to work. Is there a better tutorial?
Add an ellipsis to the VM argument. Instead of:
-enableassertions:javaapplication58
use
-enableassertions:javaapplication58...
There's a pretty good overview of Java assertions here. It describes enabling/disabling assertions for specific classes within a package, and some other tips and tricks to help you along.
You need to use this with three dots to get everything in the package
-enableassertions:javaapplication58...
That will also enable assertions in subpackages unless you explicitly disable them.
From the output of java -h
one gets this help:
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
精彩评论