protobuf
protobuf copied to clipboard
CompileOnly protobuf-java failed to run
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?
- create a module as A,
- 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'
}
- 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);
}
}
- create a module as B
- 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'
}
- 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();
}
}
- run
Test.java

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?