Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java why is my jar file so big?

Tags:

java

I have a pretty simple (And short) program that when compiled is somehow 26.9 MB?

I used a online java decompiler and all this came up: imgur link

My program uses one java file which is main.java:

}

Why is my program so large?


2 Answers

As said above, IntelliJ will package the dependencies into the created JAR file. You can see this by extracting the JAR jar xf file.jar file and taking a look in the contents.

What you can do is (as suggested) use Maven (https://maven.apache.org/install.html) this will allow you to create Dependencies that you can then supply to your code on the classpath.

With IntelliJ click File > New > Project,

Pick Maven from the list and choose a Project SDK.

Next give your project a Group ID (com.company) and Artifact ID (myproject); leave Version as is for now.

Now, give you project a local name (this will be the folder that gets created on your IdeaProjects directory.

IntelliJ will popup a message to you saying that "Maven projects need to be imported", I always Enable Auto-Import and then just maintain the pom.xml file.

This will create you a project, find you pom.xml file: it will look something like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.53.1</version><!-- Pick a different version if needed -->
        </dependency> 
    </dependencies>
</project>

Now you can package your project by running mvn package from the terminal, and your jar file will be saved into a target directory. It will be very small as it will contain only your code.

To run the program, you can then do the following:

mvn exec:java -Dexec.mainClass="com.company.Test" -Dexec.args="space separated arguments"

like image 142
Gavin Harris Avatar answered Oct 21 '25 06:10

Gavin Harris


It seems that you're using a lot of external libraries (now when you provided the code seems that they pulled by Selenium) in your code and intellij kindly packs them together into one jar, so you can run your program without need to care about class path in runtime.

To reduce the size of your jar I can suggest the following approaches:

  1. Reduce amount of external libraries (imported classes). For example don't include java.io.* but only the class you're actually using.

  2. Build proper compilation script using ant or maven. Then your jar will include only your classes and will be very small relatively to what you have now. However to run your code you'll need to care about having all required libs in runtime.

like image 30
Robert Navado Avatar answered Oct 21 '25 07:10

Robert Navado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!