Giter Site home page Giter Site logo

jarshrink's Introduction

JarShrink

Shrinks JARs by removing redundant class-files.

Motivation

Looking for a way to statically link libraries in Java as you would do in C/C++, I was surprised to find no proper way.
So the next best thing that came to mind was to look for tools that would at least remove unused classes from a jar. Amazed by how slowly ProGuard does it's job, I wrote this clean simple tool.

How to use

You can either run JarShrink as a commandline tool or integrate it into your software by importing it and calling it's API.
Instructions on how to use these interfaces are found in the sections further below.

If your software or software you're including makes use of Reflection, you will most likely need to tell JarShrink to keep certain packages or whole libaries in order to preserve their functionality. I've started compiling a table of known libaries with information regarding their compatibility with JarShrink further below this readme.

There are pre-built JARs in the release-section, so you won't necessarily have to build JarShrink yourself.
However there is also a python build-script.

Prequisites

  • Java 8 or higher
  • JDK to run from commandline or any implementation of jdeps for API use

Commandline Interface

Grammar

jarShrink <jarFile> [<argumentName> <argumentValue>]

Arguments

Argument Value Effect
-o or -out directory Specifies the output-file for the newly created jar.
-k or -keep package or class Specifies a package or class that will be retained together with it's dependencies.
Can be called multiple times.
-s or -status Print status information while processing.
-n or -nolist Don't print a list of the remaining dependencies.
-t or -tmp directory Specifies JarShrinks tmp directory

Example

jarShrink "my.jar" -out "my_shrunken.jar" -status -keep "some.package.with.reflection"

API

JarShrink's functionality is encapsulated in the class JarShrinker for API use.

Examples

A minimal example would be:

JarShrinker shrinker = new JarShrinker();
shrinker.shrink(jarFile, outFile);

A more complete example:

JarShrinker shrinker = new JarShrinker(tmpDir);
shrinker.setPrintStatus(true);
shrinker.setPrintDependencyList(true);

shrinker.shrink(jarFile, outFile, keeps);

How it works

A basic summary of JarShrink's procedure:

  • Extract the jar's contents into a temporary directory.
  • Use jdeps to generate a dependency-map of all classes inside the jar.
  • Search for a Main-Class specified in the MANIFEST.MF file.
  • Construct a Dependency-Tree with the Main-Class and/or the specified classes/packages to keep as it's root.
  • Remove all class-files from the temporary directory that aren't in Dependency-Tree and scrap folders that are now empty.
  • Build a new jar from the remaining contents of the temporary directory.

Compatibility with known libraries

Below is a table of known libraries and how well JarShrink does with them.
Note that any library will still work if imported as a jar file.

Library Compatibility Extra arguments (if needed)
Dom4j
Guava
HTMLUnit X
jbzip2
JInput (✓) -keep net.java.games.input
jorbis
libjpeg-turbo
LWJGL
Slick2D

A few notes

  • JarShrink won't touch included .jar files. This is not due to lazyness but to retain the ability to make sure that libraries, that make use of reflection will still keep their full functionality.

jarshrink's People

Contributors

bspar avatar deconimus avatar stechio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.