Backend Development 5 min read

Resolving IllegalAccessException When Reflectively Invoking a Static Method in a Java Test Framework

The article details a Java reflection issue where invoking a static test method via ExecuteSource caused IllegalAccessException, explains the root cause related to method visibility, and shows how adding the public modifier fixes the problem, enabling successful distributed performance testing.

FunTester
FunTester
FunTester
Resolving IllegalAccessException When Reflectively Invoking a Static Method in a Java Test Framework

While extending a distributed performance testing framework, the author tried to invoke a test method packaged in a JAR using reflection via com.funtester.frame.execute.ExecuteSource#executeMethod(java.lang.String, java.lang.Object...) , but encountered errors during execution.

The log showed a NoSuchMethodException followed by an IllegalAccessException indicating that com.funtester.frame.execute.ExecuteSource could not access a static member of com.funtest.javatest.FunTester :

INFO-> 当前用户:oker,工作目录:/Users/oker/IdeaProjects/funtester/,系统编码格式:UTF-8,系统Mac OS X版本:10.16
WARN-> 方法属性处理错误!
java.lang.NoSuchMethodException: com.funtest.javatest.FunTester.test()
    at java.lang.Class.getMethod(Class.java:1786)
    ...
WARN-> 反射执行方法失败:com.funtest.javatest.FunTester.test
java.lang.IllegalAccessException: Class com.funtester.frame.execute.ExecuteSource can not access a member of class com.funtest.javatest.FunTester with modifiers "static"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    ...
Process finished with exit code 0

The original test class defined the test method as static without the public keyword, which caused the reflective call to fail because the method was not accessible from another class.

package com.funtest.javatest;

import com.funtester.frame.SourceCode;
import com.funtester.frame.execute.ExecuteSource;
import java.io.IOException;

public class FunTester extends SourceCode {
    public static void main(String[] args) throws IOException {
        ExecuteSource.executeMethod("com.funtest.javatest.FunTester.test");
    }

    static void test() {
        output("FunTester成功了!");
    }
}

Realizing that Groovy allows omission of the public keyword, the author added the missing modifier, changing the method to public static void test() :

public static void test() {
    output("FunTester成功了!");
}

After this modification, the console output confirmed successful execution:

INFO-> 当前用户:oker,工作目录:/Users/oker/IdeaProjects/funtester/,系统编码格式:UTF-8,系统Mac OS X版本:10.16
INFO-> FunTester成功了!

Process finished with exit code 0

The fix resolves the reflection access issue, allowing the distributed performance testing scripts to run correctly.

JavareflectionPerformance Testingtesting frameworkstatic methodillegalaccessexception
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.