protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

CompileOnly protobuf-java failed to run

Open EarthChen opened this issue 2 years ago • 0 comments

What version of protobuf and what language are you using?

compileOnly group: 'com.google.protobuf', name: 'protobuf-java', version: '3.21.12'

Language: Java

What operating system (Linux, Windows, ...) and version?

Macos

What runtime / compiler are you using (e.g., python version or gcc version)

What did you do?

  1. create a module as A,
  2. add compileOnly protobuf-java dependency
dependencies {
    compileOnly group: 'com.google.protobuf', name: 'protobuf-java', version: '3.21.12'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
  1. create a ProtobufDemo.java file in module A
public class ProtobufDemo {

    public void test() {
        Message message = Any.newBuilder().build();
        // System.out.println(message);
        Any.pack(message);
        // System.out.println(any);

    }
}
  1. create a module as B
  2. add module A as dependency
dependencies {
    compile(project(":A"))
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
  1. create a Test.java file in module B
public class Test {

    @SneakyThrows
    public static void main(String[] args) {
        Class.forName( "org.example.lib.ProtobufDemo");
        // ProtobufDemo protobufDemo = new ProtobufDemo();
        // protobufDemo.test();
    }
}
  1. run Test.java
image

if we change ProtobufDemo.java file like this

public class ProtobufDemo {

    public void test() {
        Message message = Any.newBuilder().build();
        // avoid opt
        System.out.println(message);
        Any any = Any.pack(null);
        // avoid opt
        System.out.println(any);
    }
}

then run Test.java will fine

What did you expect to see

no error


In my understanding, the class in the method should not be verified during the class loading process, why is there an error?

EarthChen avatar Dec 21 '22 08:12 EarthChen