Reputation: 13556
I would like to inspect the contents of a resource file of a java module of my locally installed jvm from the command line.
Based on a question about mime type support in a jvm I would like to see the contents of /sun/net/www/content-types.properties
in module java.base
of my locally installed jvm.
However when scrolling through the files installed for the jvm i was unable to find a file called like java.base.*
. However I see a file ./lib/modules
being over 130 MB in size. I suspect that this file contains the contents of module java.base
. But 7-zip is unable to open this file as an archive.
How can I see the contents of /sun/net/www/content-types.properties
in module java.base
of my locally installed jvm using the command line?
Upvotes: 4
Views: 761
Reputation: 46116
I'm not aware of a command-line tool that you can use for this, though that doesn't mean one doesn't exist. However, you can write a quick utility program to get the contents of the resource in less than 20 lines of code.
Note all three approaches demonstrated, as currently implemented, read the resource from the JDK which is executing the program. Also, you can obviously modify the code of either approach to better fit your needs. Perhaps by making them more "dynamic" (e.g., accept command-line arguments).
One option is to use the resource API:
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
var name = "/sun/net/www/content-types.properties";
try (var in = Object.class.getResourceAsStream(name)) {
// Thanks to Holger for the code simplification
in.transferTo(System.out);
}
}
}
Note: The getResourceAsStream
method is being called on a class that's in the java.base
module.
Then you'd run the above like so (making use of the "single source file" feature):
java --add-opens=java.base/sun.net.www=ALL-UNNAMED Main.java
ModuleReader
import java.io.IOException;
import java.lang.module.ModuleFinder;
public class Main {
// Thanks to Holger for pointing out this approach
public static void main(String[] args) throws IOException {
var name = "sun/net/www/content-types.properties";
var mod = ModuleFinder.ofSystem().find("java.base").orElseThrow();
try (var reader = mod.open();
var in = reader.open(name).orElseThrow()) {
in.transferTo(System.out);
}
}
}
With this approach, you don't need the --add-opens
argument.
A third option is to use the JRT file system and read the resource like a "file":
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
var uri = URI.create("jrt:/java.base/sun/net/www/content-types.properties");
try (var in = Files.newInputStream(Path.of(uri))) {
// Thanks to Holger for code simplification
in.transferTo(System.out);
}
}
}
With this option, you don't need the --add-opens
argument.
Upvotes: 4