java -Djava.ext.dirs= -jar 启动项目,产生的问题

在使用`-Djava.ext.dirs`启动Java项目时遇到SSLKeyException和NoSuchMethodError。解决方案包括修改启动脚本、理解-D参数对加载路径的影响,以及通过调整pom.xml配置确保配置文件正确加载。最终,通过在MANIFEST.MF中指定classpath解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题一:

我在shell脚本中用:
nohup java -Djava.ext.dirs=${DIR}/ -jar ${DIR}/${JAR_NAME} > ${DIR}/nohup.out &
启项目,请求https报了以下错误:

Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error

问题二:

为解决问题一,将:
jre目录下的/lib/ext/sunjce_provider.jar加到自己的jar包同级目录后

启动报以下错误:

Caused by: java.lang.NoSuchMethodError: sun.security.internal.spec.TlsMasterSecretParameterSpec.getExtendedMasterSecretSessionHash()

并且程序直接退出。

最后:

改启动脚本如下:

nohup java -Djava.ext.dirs=${DIR}/:${JRE_DIR}/lib/:${JRE_DIR}/lib/ext/ -jar ${DIR}/${JAR_NAME} > ${DIR}/nohup.out &

问题全部解决了

总结原因:

1.由于我打包方式的问题,导致配置文件没有加载到classpath目录下,为了解决配置文件加载问题我用了java -Djava.ext.dirs=${DIR}/ -jar ${DIR}/${JAR_NAME},但是这样一来java自带的一些加密解密的jar包,就不会自动加进来了,详情可以百度。

2.-Djava.ext.dirs=${DIR}/:${JRE_DIR}/lib/:${JRE_DIR}/lib/ext/中:

${DIR}/是jar所在目录,

${JRE_DIR}/lib/:${JRE_DIR}/lib/ext/引得就是eclipseJRE System Libraryjar

如图:

eclipse中JRE System Library的jar
如上图,这些jar全部来自jre目录下的 /lib/和/lib/ext/目录,只要将这两个目录的路径配到-Djava.ext.dirs参数中就能解决问题

最后的最后:

其实你不用-Djava.ext.dirs参数,就不会有上面的问题(不用-Djava.ext.dirs参数,java -jar启动的时候,会自动加载 eclipseJRE System Library显示的这些jar),但还会有配置文件加载的问题,可以用 java -cp代替,最好的解决方式如下,在pom中加入以下plugin的配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <!-- 把jar包所在目录和jar包所在目录下的config目录加到manifest的classpath中 -->
                <Class-Path>. ./config/</Class-Path>
            </manifestEntries>
        </archive>
        <excludes>
            <exclude>lib/**</exclude>
            <exclude>templates/**</exclude>
            <exclude>config/**</exclude>
            <exclude>*.properties</exclude>
            <exclude>*.config</exclude>
            <exclude>*.json</exclude>
            <exclude>*.xml</exclude>
            <exclude>*.proto</exclude>
            <exclude>*.yml</exclude>
            <exclude>mapper/**</exclude>
            <exclude>doc/**</exclude>
        </excludes>
    </configuration>
</plugin>

pom加入以上配置,就是把jar包所在目录和jar包所在目录下的config目录加到manifest的classpath中。
这样配置后打包生成的MANIFEST.MF文件如下:
MANIFEST.MF文件
这样你读额外的配置就可以用ClassLoader(类加载器)读取到了

/app/jdk1.8.0_192/bin/java -cp /app/BES-CLUSTER-951/node-172.16.100.235/patch/V9.5.1.2539.002.jar:/app/BES-CLUSTER-951/node-172.16.100.235/patch/V9.5.1.2539.001.jar:/app/BES-CLUSTER-951/node-172.16.100.235/lib/*:/app/BES-CLUSTER-951/node-172.16.100.235/lib/3rd/*: -Dcom.bes.enterprise.stopAgentTimeout=180 -Dclient.ssl.keyStorePassword={AES}PrzBD+FLE0Wheq7AAaghXw== -Dcom.bes.enterprise.nodeagent.process.timeout=180 -Dclient.ssl.keyStore=${com.bes.installRoot}/conf/security/client.p12 -Dclient.ssl.keyStoreType=PKCS12 -Dcom.bes.enterprise.startAgentTimeout=180 -Dcom.bes.enterprise.async.event.retainResultTimeout=300 -Djava.security.egd=file:/dev/./urandom -XX:+UnlockDiagnosticVMOptions -XX:MetaspaceSize=1028m -XX:NewRatio=2 -XX:HeapDumpPath=/toptdata/besServerLog/node-172.16.100.235/logs/dump/ -XX:LogFile=/toptdata/besServerLog/node-172.16.100.235/logs/jvm.log -XX:+HeapDumpOnOutOfMemoryError -XX:-UseVMInterruptibleIO -XX:MaxMetaspaceSize=2048m -XX:+LogVMOutput -Xmx4096m -Xms2048m -server -Djava.endorsed.dirs=/app/BES-CLUSTER-951/node-172.16.100.235/lib/endorsed -Dcom.bes.javaRoot=/app/jdk1.8.0_192 -Dcom.bes.installRoot=/app/BES-CLUSTER-951/node-172.16.100.235 -Dcom.bes.instanceRoot=/app/BES-CLUSTER-951/node-172.16.100.235 -Djava.awt.headless=true -Djava.ext.dirs=/app/jdk1.8.0_192/lib/ext:/app/jdk1.8.0_192/jre/lib/ext:/app/BES-CLUSTER-951/node-172.16.100.235/lib/ext -Djava.net.preferIPv4Stack=true -Djava.library.path=/app/BES-CLUSTER-951/node-172.16.100.235/lib:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib com.bes.enterprise.nodeagent.bootstrap.NodeAgentMain start --startinstances=false --restartinstances=true --monitorinterval=5 --syncinstances=true -instancedir /app/BES-CLUSTER-951/node-172.16.100.235 -verbose false -debug false -nodename node-172.16.100.235 -read-stdin true
06-02
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值