1

From: Why do we use rt.jar in a java project?

I get that you need rt.jar to be able to use a base class library such as java java.util.

There is no "rt.jar" in the new java SDK. If I were to have a .class file compiled from a .java file that uses a library from the java base class libraries such as java.util, how would I be able to run it if I can't reference "rt.jar"?

From my understanding when making a runnable jar file you use classpath to reference other libraries used in the jar. How can I reference the base class libraries when making a jar now that there is no "rt.jar"?

  • When the _Java Platform Module System_ was added in Java 9, the JDK was modularized. There is no `rt.jar` (or `tools.jar`) anymore. The standard Java classes are contained in the "run-time image", which is located in the `lib/modules` file (at least in Java 18 on Windows). You can add your own modules to this file in a custom run-time image using `jlink`. – Slaw Jul 02 '22 at 17:17
  • But even before Java 9, you never had to add `rt.jar` to the class-path, as it was implicitly on the class-path. This is still true in Java 9+ (though the modules are implicitly on the _module-path_ now, instead of the _class-path_). In other words, for classes in the Java standard library, you don't have to use any special class-path/module-path configurations in order to use them. Just add the necessary imports to your source file. – Slaw Jul 02 '22 at 17:18
  • @Slaw How does implicit work? Does the jvm automatically make the modules and "rt.jar", for older versions, available to any class or jar file that runs in it? – user19468061 Jul 02 '22 at 18:07
  • @Slaw Eclipse is referencing a jar called "jrt-fs". I am confused because if the base class libraries are implicitly in the modular/class paths why is eclipse explicitly pointing to this jar in the build path? Or Is eclipse just saying that they're there without actually doing anything, like configuring the module-path or class-path(for older versions) to include them? – user19468061 Jul 02 '22 at 18:28
  • Yes, any classes in `rt.jar` (older versions) and the modules in the run-time image (newer versions) are automatically available. As for `jrt-fs.jar`, that is related to the "Java Runtime File System", which is an implementation of `java.nio.file.FileSystemProvider` that provides a way to read the run-time image as if they were part of a file system. The `jrt-fs.jar` is compiled in such a way as to be compatible with Java 8 (JRT was added in Java 9, as part of the module system). This allows **tools** running on Java 8 to inspect the run-time image of JDK versions >= 9. – Slaw Jul 02 '22 at 18:38
  • To be clear, the classes in `jrt-fs.jar` are _also_ in the run-time image (i.e., in the `modules` file). But those classes are compiled to target the version of the JDK. The `jrt-fs.jar` is just a separate compilation of the same classes but targeted for Java 8, and is specifically for use of tools (such as IDEs) running on Java 8. Code compiled for newer versions of Java cannot be executed by older versions of Java. – Slaw Jul 02 '22 at 18:41
  • @Slaw So for the "ModulePath" under the libraries tab for the Java Build Path in Eclipse when it shows "JRE System Library", all Eclipse is doing is using the `jrt-fs.jar` to read the "run-time image" in the jdk? Meaning it isn't actually doing anything like including it in the "modulepath", it's just showing the current "Java-SE" and its libraries? – user19468061 Jul 02 '22 at 19:45

1 Answers1

0

Look into the jmods folder of your jdk installation - the .jmod files contain the modules of the JDK (each contains .class and/or .so files).

You can use the bin/jmod tool to list contents, extract, or create .jmod files. If you need to inspect the contents of the .class files that correspond to .java source files from the JDK, you can extract the .class files from these .jmod files. For example, the java.util classes are in the java.base.jmod module file.

axel22
  • 32,045
  • 9
  • 125
  • 137