Search code examples
javanosuchmethoderrorbtrace

strange things with btrace and java.lang.NoSuchMethodError


the main class:

package com.xxx.yyy;

public class Hello{
    public static void main(String[] args){
        A a = new A();
        while(true){
            try {
                a.execute(1000);
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

class A:

package com.xxx.yyy;

public class A{
    public void execute(int sleepTime) throws Exception {
        System.out.println("sleep time is "+sleepTime); 
    }
}

btrace script:

import static com.sun.btrace.BTraceUtils.println;  
import static com.sun.btrace.BTraceUtils.str;  
import static com.sun.btrace.BTraceUtils.strcat;  
import static com.sun.btrace.BTraceUtils.timeMillis;  

import com.sun.btrace.annotations.BTrace;  
import com.sun.btrace.annotations.Kind;  
import com.sun.btrace.annotations.Location;  
import com.sun.btrace.annotations.OnMethod;  
import com.sun.btrace.annotations.ProbeClassName;  
import com.sun.btrace.annotations.ProbeMethodName;  
import com.sun.btrace.annotations.TLS;
@BTrace
public class BtraceTest{

    @OnMethod(clazz="com.xxx.yyy.A",method="execute",location=@Location(Kind.RETURN))
    public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,int sleepTime){
        println(strcat("the class name=>", name));  
        println(strcat("the class method=>", method));  
        println(strcat("the class method params=>", str(sleepTime)));
    }
}

everything is right. BUT: when I move the line Thread.sleep(1000) to class A's execute function, like this:

package com.xxx.yyy;

public class A{
    public void execute(int sleepTime) throws Exception {
        System.out.println("sleep time is "+sleepTime); 
        Thread.sleep(1000);
    }
}

the NoSuchMethodError is thrown by Hello.

Exception in thread "main" java.lang.NoSuchMethodError: com.xxx.yyy.A.$btrace$BtraceTest$traceExecute(Ljava/lang/String;Ljava/lang/String;I)V
    at com.xxx.yyy.A.execute(Unknown Source)
    at com.xxx.yyy.Hello.main(Hello.java:8)

my environment is

java version "1.8.0_121"
BTrace v.1.3.9 (20170111)

anyone can explain why?thanks!


Solution

  • I think your class should implements Runnable or extends Thread. Then only you can use start, sleepand such similar methods in your program. Have a look at here to know how to implement thread methods.