Menu

Nakov.com logo

Thoughts on Software Engineering

Extract All Classes Loaded in the JVM into a Single JAR

Today I needed to optimize the file size for a commercial Java applet (Web form signer) that my team is developing in the last month. We use few external libraries (Bouncy Castle Crypto API and few others) and these libraries are above 2 MB in several JAR files. If we were developing server side application, it would not be a problem, but when we are building an applet, the size of the applet and all its JARs matters.

I needed to remove all unused classes from the JARs that my applet was including a part of itself. For example the Bouncy Castle JARs were about 1,6 MB but the applet used only a small part of all algorithms and standards implemented by these JARs.

Extracting All Classes Loaded in the JVM

My final goal was not only to remove all unused classes from all JAR files but also merge these JARs along with the applet classes into a single JAR file that has the smallest possible size. I came with the idea to run the applet, to go through all its functionality and to get a list of all classes currently loaded into the JVM executing the applet. At this moment all classes required by the applet for its normal work will be loaded in the JVM and all classes that was never used by the applet will not be loaded in the JVM. If I package all these classes into a new JARs, it will contain the minimal set of classes nedded by the applet along with the applet classes.

As fas as I know how the JVM and the class loaders behave, this should be correct – we can expect all classes required by the applet to be loaded in the JVM after its entire functionality is accessed at least once.

I had a serious problem: how to get a list of all classes loaded in the JVM.

List All Classes Loaded in the JVM

Geting a list of all classes that are loaded in the JVM at some moment is not easy job. We can write Java agent through the java.lang.instrument API but I needed to do this at runtime (just to add few lines to the applet). I found in Google a very nice class for accessing all classes loaded in the JVM written by Vladimir Roubtsov and published in Java World (http://www.javaworld.com/javaworld/javaqa/2003-07/01-qa-0711-classsrc.html). With few modifications it successfully listed all classes loaded in my applet.

Create a Single JAR with All Classes Loaded in the JVM

The next step was to create a single JAR with all classes loaded in the JVM. This was not complex. I created a class with few methods for copying all currently loaded classes into some directory specified as parameter. Here is the source code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

/***
 * This class extracts all classes loaded in the JVM and their binary contents
 * (.class files) into given directory so that you can create JAR archive later.
 * @author Svetlin Nakov - [url]
 */
public class AllClassesInJVMExtractor {

	private final static int BUFFER_SIZE = 4096;

	public static void extractAllClassesFromJVM(String destFolder)
			throws IOException {
		ClassLoader appLoader = ClassLoader.getSystemClassLoader();
		ClassLoader currentLoader = AllClassesInJVMExtractor.class.getClassLoader();

		ClassLoader[] loaders = new ClassLoader[] { appLoader, currentLoader };
		final Class< ?>[] classes = ClassScope.getLoadedClasses(loaders);
		for (Class< ?> cls : classes) {
			String className = cls.getName();
			URL classLocation = ClassScope.getClassLocation(cls);
			System.out.println("Extracting class: " + className + " from " + 
					classLocation);
			String destFileName = destFolder + "/"
					+ className.replace(".", "/") + ".class";
			copyFile(classLocation, destFileName);
		}
	}

	private static void copyFile(URL sourceURL, String destFileName)
			throws IOException {
		File destFile = new File(destFileName);
		File destDirectory = destFile.getParentFile();
		destDirectory.mkdirs();
		InputStream srcStream = sourceURL.openStream();
		try {
			OutputStream destStream = new FileOutputStream(destFile);
			try {
				copyStreams(srcStream, destStream);
			} finally {
				destStream.close();
			}
		} finally {
			srcStream.close();
		}
	}

	private static void copyStreams(InputStream srcStream,
			OutputStream destStream) throws IOException {
		byte[] buf = new byte[BUFFER_SIZE];
		while (true) {
			int bytesRead = srcStream.read(buf);
			if (bytesRead == -1) {
				// End of stream reached
				return;
			}
			destStream.write(buf, 0, bytesRead);
		}
	}

}

It is not a rocket science. I go through all classes loaded by the current class loader and by the system class loader, get their fully qualified name (e.g. org.bouncycastle.cms.CMSSignedData) and their source URL location (e.g. jar:file:/C:/PROJECTS/GeneratePKCS7andVerify/lib/bcmail-jdk15-140.zip!/org/bouncycastle/cms/CMSSignedData.class) and I copy their binary contents (from the URL) to the destination folder (into a .class file). In the mean time I recreate the package structure (following the full class name with all its packages). Finally I get a directory containing all class files loaded in the JVM at the time of caling my method and I can manually package them in a JAR (removing beforehand all system Java classes). That’s all. I use slightly modified version of ClassScope.java.

You can download a fully functional example here (Eclipse project): ExtractAllClassesFromJVMIntoJAR.zip.

Comments (3)

3 Responses to “Extract All Classes Loaded in the JVM into a Single JAR”

  1. dakal says:

    I used AllClassesInJVMExtractor , but still collected classes were encrypted!
    I thought encrypted classes has to be decrypted before load in JVM, So i expected to collect original classes from memory against an encrypted jar file!!!
    Have you any idea?
    Can any body help me for gathering encrypted classes in decrypted form???
    thanks

  2. […] on the work of Svetlin Nakov (Extract classes loaded in JVM to single JAR) I did a POC which gives you the names of the loaded native libraries from the application […]

  3. […] on the work of Svetlin Nakov (Extract classes loaded in JVM to single JAR) I did a POC which gives you the names of the loaded native libraries from the application […]

RSS feed for comments on this post. TrackBack URL

LEAVE A COMMENT