0
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?

Daanish
  • 1,061
  • 7
  • 18
  • 29
  • Please use a consistent and logical indent for code blocks. – Andrew Thompson Sep 04 '12 at 04:45
  • 1
    @ are you really writing a code coverage tool? See jacoco/emma/cobertura etc. All of these instrument code with specific instruction and then collect data at run time. Could you explain your approach? – Jayan Sep 04 '12 at 04:58
  • Hi Jayan,My approach was to get the line numbers using `this.currentThread().getStackTrace()[1].getLineNumber()`.I could've instrumented my code but the application(cannot be revealed) for which I'm writing the code coverage tool needs every line executed.Hence I think instrumenting at every line will make the execution slow.So please suggest an approach.Thank you. – Daanish Sep 04 '12 at 05:32
  • 2
    @Daanish: But as long as `Hello` is not kind enough to throw an exception, you cannot catch one. And even when an exception is thrown: I don't see any reasonable way to get something like a coverage from that. – A.H. Sep 04 '12 at 05:37

3 Answers3

2

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

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
1

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

asgs
  • 3,928
  • 6
  • 39
  • 54
  • I can read the output even without Process.But my requirement is I was execution path of Hello.java not the output.Thank you @asgs. – Daanish Sep 04 '12 at 04:57
  • what do you mean by Execution path? i thought you wanted to catch the Exception Stacktrace thrown from another java process. – asgs Sep 04 '12 at 05:19
  • Sorry I might have not been clear with my question. I want the execution path of Hello.java . Hence I thought getting the StackTrace was an option. My main requirement is getting the execution path of Hello.java.Thank you. – Daanish Sep 04 '12 at 05:30
  • Sorry, but you still did NOT answer what you mean by Execution Path. Why don't you rewrite your question? Discussing this in comments section is not worth it. – asgs Sep 04 '12 at 05:37
0

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.

Daanish
  • 1,061
  • 7
  • 18
  • 29