可以让 JNI 支持类路径中的通配符扩展吗?

2022-01-25 00:00:00 c unix java java-native-interface classpath

我有一个通过 JNI 调用 Java 的 C 二进制文件.我将 CLASSPATH 设置为 somedir/* 以获取 somedir 中的所有 jar.

I have a C binary that calls out to Java via JNI. I set CLASSPATH to somedir/* to pick up all the jars in somedir.

当我运行二进制文件时,找不到所需的类定义.当我跑步时

When I run the binary, a required class definition cannot be found. When I run

java that.class's.name 

在同一命令行中,已成功找到该类.如果我将 somedir/中的所有 jar 显式添加到类路径中,一切都会很好,但这会导致我想避免的 very 长类路径.

from the same command line, the class is successfully found. If I explicitly add all the jars in somedir/ to the classpath, everything works great, but that leads to a very long classpath which I'd like to avoid.

通过 JNI 执行的 JVM 是否支持类路径的通配符扩展?可以这样做吗?

Does a JVM executed via JNI honour wildcard expansion of the classpath? Can it be made to do so?

推荐答案

我通过阅读热点源码找到了答案.

I figured out the answer by reading the hotspot source code.

只有通过 CLASSPATH-cp/-classpath 传递的路径才能进行通配符扩展.然后这些作为系统属性通过 -Djava.class.path 传递给正在运行的 JVM.

Only paths passed via either CLASSPATH or -cp / -classpath are subject to wildcard expansion. These are then passed as a system property to the running JVM via -Djava.class.path.

您通过 JVMOptions 结构告诉 JNI 调用的 JVM 一个类路径,该结构可能包括 -Djava.class.path-classpath 不会必然受到尊重(实际上,不是通过热点实现).由于 java.class.path 作为系统属性直接传递给 JVM,它不会扩展通配符,因此通配符不起作用.

You tell a JNI-invoked JVM about a classpath via a JVMOptions structure, which may include -Djava.class.path but -classpath will not necessarily be honoured (and in practice, isn't by the hotspot implementation). Since java.class.path is directly passed to the JVM as a system property, it doesn't get wildcard expanded and therefore wildcards won't work.

相关文章