Question

public class LineNum1 extends Thread{

    public synchronized void run()
         {
          try {
                Hello.main(null);
                System.out.println("Stack Trace of thread"+ this.currentThread().getName());
                System.out.println(this.currentThread().getStackTrace()[1].getLineNumber());
                System.out.println("End of Stack Trace");
               } catch (Exception e1) {
                    e1.printStackTrace();
               }
            }

     public static void main(String[] args) {

                LineNum1 t = new LineNum1();
                t.start();
            }
   }

I'm developing a code coverage tool.

Using the above program I'm executing Hello.java from here. Is there any method where I can get the control over Hello.java?

Or to make my life simpler can i get the line numbers of the executed lines(Execution path) of Hello.java?

Was it helpful?

Solution 3

I have not completed the code coverage completely yet. But I have used bytecode instrumentation to instrument the methods, hence i get the log of all the methods I've visited. This Javassist Turtorial might help you.

OTHER TIPS

You want to use the Java Debug Interface. It's the Java library that is used for writing Java tools like debuggers. It'll allow you to step through the program, query for line numbers and whatnots as you go.

There's a simple demo application for it called trace that does most of what you want already: http://docs.oracle.com/javase/1.5.0/docs/guide/jpda/trace.html

There's a lot of documentation for it, if you've got the time to read it: http://docs.oracle.com/javase/1.5.0/docs/guide/jpda/index.html

You can probably execute the second Java class as a Process and read the output yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top