Fork me on GitHub
#off-topic
<
2019-10-10
>
cfleming04:10:58

Hi folks, I have a Java 9 module question.

cfleming04:10:25

I’m trying to find the jar file containing java.sql.Timestamp from my running JRE.

cfleming04:10:44

I can get the URL from Timestamp.class.getResource("/" + Timestamp.class.getName().replace('.', '/') + ".class")

cfleming04:10:15

That gets me something like jrt:/java.sql/java/sql/Timestamp.class, but I don’t know how to convert that to a jar.

cfleming04:10:48

I can use ModuleFinder to get a ModuleReference which has a location(), but that’s just a jrt: URL again.

cfleming04:10:03

ModuleFinder.ofSystem().find("java.sql").get().location().get()

hiredman04:10:37

http://openjdk.java.net/jeps/220 seems to suggest there is no jar, instead there is something called a runtime image

cfleming04:10:33

Hmm, interesting. So I guess there’s no guaranteed mapping from a jrt:/ to a jar at all. I definitely read somewhere while investigating this that a module is either a jar or an exploded jar, but that may have been someone who didn’t know what they were talking about.

hiredman04:10:52

"The src.zip file is now in the lib directory rather than the top-level directory, and this file now includes one directory for each module in the image. IDEs and other tools that read this file will need to be updated"

cfleming04:10:40

But that’s the sources, right? I’m looking for the jar containing the actual class file.

cfleming04:10:41

> All other files and directories in the lib directory must be treated as private implementation details of the run-time system. They are not intended for external use and their names, format, and content are subject to change without notice.

cfleming04:10:28

Looks like they’re planning on replacing jar files at some future point.

cfleming04:10:27

I guess I should modify my question to better represent what I actually want to do: is it possible to create a classloader with some other classloader as a parent which contains additional system modules which may not be available to the parent?

cfleming04:10:48

In my case, java.sql?

cfleming04:10:57

Actually, I wonder if I can just pass a jrt:/ URL directly to the URLClassLoader constructor

hiredman04:10:09

I don't know, but there is also java.lang.ModuleLayer which is maybe a way to get classloaders for modules

cfleming04:10:55

Hmm, it lets me create the URLClassLoader with the jrt:/ URL, but that classloader still can’t find Timestamp.

cfleming04:10:05

I’ll look into the ModuleLayer and see if that helps.

cfleming04:10:32

Oh wow, using ModuleLayer.boot().findLoader(“java.sql”) as the parent classloader of my URLClassLoader seems to work!