Sundararajan's Weblog

Extracting a single .class file from java9 platform jimage modules file

We can use java FileSystem API from Nashorn JavaScript to extract a single .class file from java9 platform jimage modules file.

The following simple Nashorn script extracts .class of the given class name:

File: jextract.js

// This script extracts .class for a single class
// from the platform jimage ($JDK9/lib/modules) file.
// Specify module/class like java.base/java.util.Vector
if (arguments.length == 0) {
print("Usage: jjs jextract -- <module>/<classname>")
// Java types used from file system and net API
var FileSystems = Java.type("java.nio.file.FileSystems")
var Files = Java.type("java.nio.file.Files")
var URI = Java.type("java.net.URI")
var name = arguments[0]
var moduleName = name.substring(0, name.indexOf('/'))
var className = name.substring(name.indexOf('/') + 1)
var pathName = "/modules/" + moduleName + "/" +
className.replace(/\./g, '/') + ".class"
// get jrt fs file system instance
var fs = FileSystems.getFileSystem(URI.create("jrt:/"))
// read .class content
var content = Files.readAllBytes(fs.getPath(pathName))
// write to a file in current dir (using default fs)
// simple class name
var simpleName = className.substring(className.lastIndexOf('.') + 1)
Files.write(FileSystems.default.getPath(simpleName + ".class"), content)

Example commands to use the above script:

$ jjs jextract.js -- java.base/java.util.Vector
$ jjs jextract.js -- jdk.scripting.nashorn/jdk.nashorn.api.scripting.ScriptObjectMirror

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.