Solved

assert example issue

Posted on 2014-02-04
15
327 Views
Last Modified: 2014-03-24
Hi,

I am working on below example
http://www.avajava.com/tutorials/lessons/how-do-i-enable-assertions.html

i created both assertion test classes Assert3Test, Assert2Test under default package under src directory withut any package statement as Test  

When try to run from command prompt it is not running as below


C:\gp_workspace\BeanToBean\bin>java -ea Assert3Test Assert2Test
Exception in thread "main" java.lang.NoSuchMethodError: main

I did not get below output as mentioned in the link
Exception in thread "main" java.lang.AssertionError: Message from Assert3Test
        at test.three.Assert3Test.howdy(Assert3Test.java:5)
        at test.Assert2Test.main(Assert2Test.java:8)

I wonder how to run these assert test classes from eclipse directly without opening command promprt and going to bin directory manually
0
Comment
Question by:gudii9
  • 9
  • 6
15 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39834253
The problem is your command line that you are running the test with. Use this instead...
java -ea:Assert3Test Assert2Test
        ^
        |

Open in new window

The arrow above is pointing to the missing colon  :  that is required to run the test as intended. Note that you don't have that in the command line that you posted above. So instead of telling Java to enable assertions of Asset3Test (-ea:Asset3Test), you were actually telling Java to enable all assertions (-ea) and then run Asset3Test as the class that you wanted executed, but Asset3Test doesn't have a main method to run (hence the error message that you got)

To do this directly from Eclipse, the easiest way is to first just try to Run your Asset2Test class (menu Run -> Run...). This will create a default Run Configuration for you. Now go into the menu Run -> Run Configurations.. to edit that configuration and in the tabs that come up, go to "Arguments" and then in the "VM arguments" text box enter this...
-ea:Asset3Test

Open in new window

and then click on Run in the bottom right corner.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39843214
when i run as below getting below error.

C:\gp_workspace\BeanToBean\src>java -ea:Assert3Test Assert2Test
Exception in thread "main" java.lang.NoClassDefFoundError: Assert2Test
Caused by: java.lang.ClassNotFoundException: Assert2Test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Assert2Test.  Program will exit.

I do have the Assert2Test in the same location C:\gp_workspace\BeanToBean\src
similar to Assert3Test.java
please advise
AssertExample2.jpg
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39843869
You are running it from the src directory but is that where the compiled classes are? ie. is there a Asset2Test.class file in the directory that you are running the program from. The .java file is not enough you need the .class file. If it isn't there, try looking in the bin directory that should be at the same level as the src directory.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39857460
How to find the bin directory. I do not see at same as src level. please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39857492
I found bin now which is same level but i wonder why it is not showing in eclipse 'Project Explorer' view.

Also i got out put as



java -ea:test.three.Assert3Test test.Assert2Test
Exception in thread "main" java.lang.AssertionError: Message from Assert3Test
        at test.three.Assert3Test.howdy(Assert3Test.java:5)
        at test.Assert2Test.main(Assert2Test.java:8)



what is the meaning of the assertion?


Assertions can be used in JavaSW to test for the validity of certain conditions in a Java application. If a particular assertion condition is found to be false, an AssertionError will be thrown. Notice that this is an Error and not an Exception. Errors are unchecked, so you don't need to catch them. Typically with assertions, you don't catch them and you let the application terminate as a result of throwing the AssertionError. However, the AssertTest class catches the AssertionError.

i am not clear on above explanation as well?
please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 39867906
Specifically i am not clean on below statement

Typically with assertions, you don't catch them and you let the application terminate as a result of throwing the AssertionError

Please advise
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39869100
what is the meaning of the assertion
There is not a lot of meaning in it other than "it is there". Assertion are (should) really only used to catch bugs in your code (or some 3rd party code). For example, if you have an assertion that checks a variable is NOT NULL. The idea of this isn't, for example, to check if a user entered a value or not, or that some database is up and available or not. Those conditions should be properly handled by the code, ie. with normal exception handling or if/else testing, etc. The assertion that the variable is not null means that the programmer expects that that variable will always have a value, but if somehow it is null, then there is a bug in the code.

Normally with the above example, you would probably end up with a NullPointerException at some later point anyway, but the benefit of assertions is that the AssertionError can be thrown at the precise point in the code where the problem is, rather than resulting in some Exception at possibly a totally different piece of code at a later point in time, where it makes it hard to track down what is the root cause.


Specifically i am not clean on below statement

Typically with assertions, you don't catch them and you let the application terminate as a result of throwing the AssertionError
It is just saying that an AssertionError is something that the application code shouldn't try and deal with, and that it should just let that Error propagate back up to the JVM where it will cause the JVM to terminate.

Why shouldn't the application try and deal with it? Well, as I said above, you only use them when you are absolutely sure that in a normal running program, it should never happen. So if they do happen, it is because of a bug in the code, and if the programmer knew how to deal with it, then they would already know about the bug and have fixed it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Author Comment

by:gudii9
ID: 39871426
The assertion that the variable is not null means that the programmer expects that that variable will always have a value, but if somehow it is null, then there is a bug in the code.



why cannot we check above one using
with normal exception handling or if/else testing.

How above one different from normal exception. I am still not clear?


I have not udnerstood output and difference between

Assert2Test.java,AssertTest.java,Assert3Test.java

what author trying to convey?
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39872318
why cannot we check above one using
with normal exception handling or if/else testing.
Yes you could just use normal exception handling or if/else, but see below.

How above one different from normal exception.
So the main reason you might use assertions over other methods of checking is that "assertion checking can be disabled". As I mentioned previously, assertions are primarily used to help track down bugs in your code. In normal operation, the condition should never occur. However if you used an if/else, then the code would always check this condition even though it never occurs, so there is a small performance hit in order to perform the check.

With assertions, they are normally disabled and so the checks just don't happen at all, ie. there is no performance penalty. Why use them then? Well if someone was to find a bug in your app, then the app can be restarted with assertions enabled and all the checks would get executed. This may help you track down where the bug is. Then you fix the bug and run the app again without the assertions and you again have no performance hit.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39878686
That makes more sense

I have not udnerstood output and difference between

Assert2Test.java,AssertTest.java,Assert3Test.java

what author trying to convey?

Please advise on above
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39879139
AssertTest.java - Is just a simple test of assertions, one that passes and one that fails. So you see a message for the one that fails, only when assertions are enabled.

AssertTest2.java, AssertTest3.java - Is a second example that is a little more complex, but all the author is trying to convey here is that assertions can be enabled on a per package or per class basis. So since those two classes are in different packages, in the example command to run it, the author is selecting between those two classes to enable the assertions and subsequently shows the output when assertions are only enabled for each one of those classes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39880112
AssertTest.java - Is just a simple test of assertions, one that passes and one that fails. So you see a message for the one that fails, only when assertions are enabled.

I thought it should be reverse way.

since

assert true : "Condition is true, so we won't see this";
                  assert false : "If assertions are on, we'll see this";


what is the meaning of 'condition is true' in this example(does it mean assertion is enabled). What is meaning of 'assert false'(does it means assertion is disable?). If that is the case then output should be

testing...
If assertions are on, we'll see this


instead of below actual output
testing...


please advise


Most Java web developers that I know don't use assertions, but it's good to know that they exist. They probably are useful in particular fields of programming.

I also never remember using them in real time projects. which particular fields of programming they are used commonly?
0
 
LVL 7

Author Comment

by:gudii9
ID: 39890071
Some reason i was not able to access avajava website any more. any of you were able to acccess it? please advise
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39890611
No, the conditional expression in the assert statement doesn't control whether Assertions are enabled/disabled. Maybe it wasn't obvious in the first post that I made above but, it is the command line argument -ea or -ea:ClassNameGoesHere that enables Assertions (note that they are disabled by default).

So to see the assertion message in the output, 2 things need to happen... Assertions need to be enabled (as I just said above, use the -ea command line argument) AND the assertion condition needs to evaluate to false.

which particular fields of programming they are used commonly?
Personally, I wouldn't think that is specific to a field of programming at all. I think it is more the case that it is a language feature that has been in Java from the start (or at least very early on) that just isn't very widely used. While I did give that explanation above as to why they would be used, (in my experience) they just aren't used much. Probably because it would probably take a fair amount of work to use them properly and probably because most developers are in denial that their code would have bugs, so why bother putting effort in to help track them down!

any of you were able to acccess it?
And no, I can't access those tutorials anymore either. As you can see right down the bottom of the page that appears, "the domain has expired" so it is a problem with the people who run/host that site, not with your (or my) connection to it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39930503

C:\projects\workspace\testing\bin>java -ea:test.three... test.Assert2Test
C:\projects\workspace\testing\bin>java -ea:test.three.Assert3Test test.Assert2Test

What is difference between above two commands. please advice
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 50
Python Assistance 7 36
Requested array size exceeds VM limit 3 50
Java Loop 4 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now