jhunters / jprotobuf

A useful utility library for java programmer using google protobuf
Apache License 2.0
897 stars 283 forks source link

java21中无法使用预编译,且打成jar包后无法运行 #205

Open LinHuiG opened 10 months ago

LinHuiG commented 10 months ago

当我在java21中启用预编译插件时候,会报错

java.lang.reflect.InvocationTargetException
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:118)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at com.baidu.jprotobuf.mojo.PreCompileMojo$1.run (PreCompileMojo.java:326)
    at java.lang.Thread.run (Thread.java:1583)
Caused by: jodd.io.findfile.FindFileException: Scan entry error: EntryData{com.eastmoney.emstgtool.model.StgSubClientInfo'}; <--- java.lang.NullPointerException: Cannot invoke "String.getBytes(String)" because "code" is null
    at jodd.io.findfile.ClassFinder.scanEntry (ClassFinder.java:377)
    at jodd.io.findfile.ClassFinder.scanClassFile (ClassFinder.java:324)
    at jodd.io.findfile.ClassFinder.scanClassPath (ClassFinder.java:307)
    at jodd.io.findfile.ClassFinder.scanPath (ClassFinder.java:239)
    at jodd.io.findfile.ClassFinder.scanPaths (ClassFinder.java:178)
    at jodd.io.findfile.ClassScanner.scan (ClassScanner.java:35)
    at jodd.io.findfile.ClassScanner.scanDefaultClasspath (ClassScanner.java:28)
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain.main (JprotobufPreCompileMain.java:182)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at com.baidu.jprotobuf.mojo.PreCompileMojo$1.run (PreCompileMojo.java:326)
    at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.Exception: Cannot invoke "String.getBytes(String)" because "code" is null
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain$1.onEntry (JprotobufPreCompileMain.java:144)
    at jodd.io.findfile.ClassFinder.scanEntry (ClassFinder.java:375)
    at jodd.io.findfile.ClassFinder.scanClassFile (ClassFinder.java:324)
    at jodd.io.findfile.ClassFinder.scanClassPath (ClassFinder.java:307)
    at jodd.io.findfile.ClassFinder.scanPath (ClassFinder.java:239)
    at jodd.io.findfile.ClassFinder.scanPaths (ClassFinder.java:178)
    at jodd.io.findfile.ClassScanner.scan (ClassScanner.java:35)
    at jodd.io.findfile.ClassScanner.scanDefaultClasspath (ClassScanner.java:28)
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain.main (JprotobufPreCompileMain.java:182)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at com.baidu.jprotobuf.mojo.PreCompileMojo$1.run (PreCompileMojo.java:326)
    at java.lang.Thread.run (Thread.java:1583)
Caused by: java.lang.NullPointerException: Cannot invoke "String.getBytes(String)" because "code" is null
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain.createProtoFile (JprotobufPreCompileMain.java:255)
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain.access$5 (JprotobufPreCompileMain.java:238)
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain$1.onEntry (JprotobufPreCompileMain.java:136)
    at jodd.io.findfile.ClassFinder.scanEntry (ClassFinder.java:375)
    at jodd.io.findfile.ClassFinder.scanClassFile (ClassFinder.java:324)
    at jodd.io.findfile.ClassFinder.scanClassPath (ClassFinder.java:307)
    at jodd.io.findfile.ClassFinder.scanPath (ClassFinder.java:239)
    at jodd.io.findfile.ClassFinder.scanPaths (ClassFinder.java:178)
    at jodd.io.findfile.ClassScanner.scan (ClassScanner.java:35)
    at jodd.io.findfile.ClassScanner.scanDefaultClasspath (ClassScanner.java:28)
    at com.baidu.jprotobuf.mojo.JprotobufPreCompileMain.main (JprotobufPreCompileMain.java:182)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at com.baidu.jprotobuf.mojo.PreCompileMojo$1.run (PreCompileMojo.java:326)
    at java.lang.Thread.run (Thread.java:1583)

当我关闭预编译插件后运行在idea中运行正常,但是当我把它打成jar包后(编译正常)执行会报错

Caused by: java.lang.ExceptionInInitializerError: null
    at com.baidu.bjf.remoting.protobuf.ProtobufProxy.doCreate(ProtobufProxy.java:263) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:174) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:155) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:128) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.ProtobufProxy.create(ProtobufProxy.java:116) ~[jprotobuf-2.4.20.jar!/:na]
    at com.eastmoney.emstgtool.monitor.StgStatusDB.<init>(StgStatusDB.java:38) ~[!/:0.0.1]
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) ~[na:na]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:197) ~[spring-beans-6.1.3.jar!/:6.1.3]
    ... 38 common frames omitted
Caused by: java.lang.RuntimeException: nested:\D:\Code\em-stg-tool\em-stg-tool\target\em-stg-tool-0.0.1.jar not exist
    at com.baidu.bjf.remoting.protobuf.utils.ZipUtils.unZip(ZipUtils.java:55) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.<init>(JdkCompiler.java:161) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.utils.compiler.JdkCompiler.<init>(JdkCompiler.java:103) ~[jprotobuf-2.4.20.jar!/:na]
    at com.baidu.bjf.remoting.protobuf.utils.JDKCompilerHelper.<clinit>(JDKCompilerHelper.java:33) ~[jprotobuf-2.4.20.jar!/:na]
    ... 48 common frames omitted

我使用的是maven,pom配置如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.eastmoney</groupId>
    <artifactId>em-stg-tool</artifactId>
    <version>0.0.1</version>
    <name>em-stg-tool</name>
    <description>em-stg-tool</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.baidu</groupId>
            <artifactId>jprotobuf</artifactId>
            <version>2.4.20</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>com.baidu</groupId>-->
<!--            <artifactId>jprotobuf-precompile-plugin</artifactId>-->
<!--            <version>2.2.9</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.rocksdb</groupId>
            <artifactId>rocksdbjni</artifactId>
            <version>8.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
<!--            <plugin>-->
<!--                <groupId>com.baidu</groupId>-->
<!--                <artifactId>jprotobuf-precompile-plugin</artifactId>-->
<!--                <version>2.2.9</version>-->
<!--                <configuration>-->
<!--                    <filterClassPackage>com.eastmoney.emstgtool.model</filterClassPackage>-->
<!--                    <generateProtoFile>true</generateProtoFile>-->
<!--                    <compileDependencies>true</compileDependencies>-->
<!--                </configuration>-->
<!--                <executions>-->
<!--                    <execution>-->
<!--                        <phase>compile</phase>-->
<!--                        <goals>-->
<!--                            <goal>precompile</goal>-->
<!--                        </goals>-->
<!--                    </execution>-->
<!--                </executions>-->
<!--            </plugin>-->
            <plugin>
                <groupId>org.graalvm.buildtools</groupId>
                <artifactId>native-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.eastmoney.emstgtool.EmStgToolApplication</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
LinHuiG commented 10 months ago

Jprotobuf是否能支持java21呢?

jhunters commented 10 months ago

你使用的是哪一个jdk? openjdk还是oracle的, 后续跟进一下这个版本

LinHuiG commented 10 months ago

你使用的是哪一个jdk? openjdk还是oracle的, 后续跟进一下这个版本

我试了graalvm-community-jdk-21.0.2以及oracle openJdk 21,现象一致

jhunters commented 10 months ago

oracle openJdk 21, 没有复现你的问题。 方便把你测试的代码发一下吗? 另外看日志像是出错在 生成pb定义文件上,可以在maven中, 修改 false 测试一下是否还有同样问题

LinHuiG commented 8 months ago

oracle openJdk 21, 没有复现你的问题。 方便把你测试的代码发一下吗? 另外看日志像是出错在 生成pb定义文件上,可以在maven中, 修改 false 测试一下是否还有同样问题

修改为false后编译通过了,经过测试,发现还是和类名导致的编译顺序有关系 当我声明A和B两个类的时候,我如果在A类中引用了B类,那么编译会报错,反之我如果在B类中引用了A类,则编译不会报错。

LinHuiG commented 8 months ago

至于打包成jar包后执行,则都无法执行,这是源代码

无法编译: Jprotobuf-java-21.zip

改类名后可编译: Jprotobuf-java-21.zip

编译后java -jar 无法运行,会报错

jhunters commented 6 months ago

fixed at version 2.4.23