Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify which lines of code participated in a specific execution of a Java program?

Suppose that I have a Java program within an IDE (Eclipse in this case). Suppose now that I execute the program and at some point terminate it or it ends naturally.

Is there a convenient way to determine which lines executed at least once and which ones did not (e.g., exception handling or conditions that weren't reached?)

A manual way to collect this information would be to constantly step with the debugging and maintain a set of lines where we have passed at least once. However, is there some tool or profiler that already does that?

Edit: Just for clarification: I need to be able to access this information programmatically and not necessarily from a JUnit test.

like image 855
Uri Avatar asked Nov 14 '08 04:11

Uri


People also ask

What is the correct sequence of execution of any Java program?

The execution order of the program is that the static block executes first, then instance block, and then constructor.

How a Java program is compiled and executed?

In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then executes at runtime. Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension .

How many types of execution are there in Java?

Java, being a platform-independent programming language, doesn't work on the one-step compilation. Instead, it involves a two-step execution, first through an OS-independent compiler; and second, in a virtual machine (JVM) which is custom-built for every operating system.

What is the starting point of execution in Java?

12.1. Java Virtual Machine Startup. The Java Virtual Machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings. In the examples in this specification, this first class is typically called Test .


2 Answers

eclemma would be a good start: a code coverage tool would allow a coverage session to record the information you are looking for.

alt text
(source: eclemma.org)

like image 73
VonC Avatar answered Oct 12 '22 23:10

VonC


What you're asking about is called "coverage". There are several tools that measure that, some of which integrate into Eclipse. I've used jcoverage and it works (I believe it has a free trial period, after which you'd have to buy it). I've not used it, but you might also try Coverlipse.

like image 32
Ian Varley Avatar answered Oct 13 '22 00:10

Ian Varley