Explain Codes LogoExplain Codes Logo

How to use classes from .jar files?

java
classpath
java-8
best-practices
Alex KataevbyAlex Kataev·Feb 22, 2025
TLDR

Integrate a .jar file into your Java project by adding it to the classpath. For a quick command-line approach:

// "Hey Java compiler, check this place out for classes!" javac -cp "path/to/jarfile.jar" YourApp.java // "Hey JVM, don't forget about our good friend .jar!" java -cp ".;path/to/jarfile.jar" YourApp

In an IDE, you'll need to add the JAR to your project's build path. If you use Maven or Gradle, you declare the JAR in your pom.xml or build.gradle respectively. For Maven:

<dependency> <groupId>your.groupid</groupId> <artifactId>your-artifact</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/path/to/jarfile.jar</systemPath> </dependency>

Now, your compiler and JVM know where to find the classes within your shiny new JAR.

Nitty-gritty details

Understanding classpath

The classpath is like the JVM's personal GPS. It tells your compiler and JVM where to look for .class files and resources when compiling and running your program. Be cautious about the CLASSPATH environment variable — it's known to cause hiccups.

// Use this on Unix/Linux java -cp ".:/path/to/jarfile.jar" // And this one on Windows java -cp ".;\path/to/jarfile.jar"

Remember: colons for Linux, semicolons for Windows.

Importing JARs in your IDE

Most IDEs have streamlined importing process for JARs. Teaming up with IntelliJ? Right-click the project > Properties > Java Build Path > Libraries > Add JARs/Add External JARs. Now the IDE takes care of classpath magic!

Raising an executable JAR

At times, you might want to pack your program inside a JAR and make it executable. The META-INF/MANIFEST.MF within the JAR must mention the Main-Class. The JVM looks for this when you run java -jar.

A JAR of your own

When creating a .jar of your application, the MANIFEST.MF can also reference internal or external dependencies. It allows you to conveniently package everything together.

Writing a right-fit manifest

Your MANIFEST.MF should look like this:

Class-Path: httpclient-4.5.2.jar httpcore-4.4.4.jar commons-logging-1.2.jar
Main-Class: com.yourmainclass.Main

Intricacies and possible pitfalls

Dealing non-executable JARs

Non-executable JARs are like modern art. They've got valuable contents (classes), but no direct way to run them. Seems a bit unconventional, right?

Filing it right

Make sure the paths to .jar files in your command-line invocations are as correct as an umpire's rulebook. Full paths are your best friend if the .jar is in another directory.

-cp vs -classpath

The -cp flag is an alias for -classpath. This choice is as important as choosing between ketchup or mayo. Both work fine, prefer whichever commands your palate -

javac -classpath "path/to/jarfile.jar" YourApp.java java -classpath ".;path/to/jarfile.jar" YourApp

Practical storage spots

Store your .jar files in sensible locations. It's like choosing the right parking spot for your car — closer to the destination, the better!

Special scenarios

Maven dependencies

In Maven, specify dependencies in your pom.xml. Maven, the loyal butler, fetches them from online repositories.

Gradle: Your build initiator

With Gradle, add dependencies in build.gradle:

dependencies { // Here's a secret potion for JARs implementation files('libs/jtwitter.jar') }

This simplifies handling both project-specific and external dependencies.

Continuous integration sadness

When setting up CI (Continuous Integration) systems, remember those .jar dependencies. If not referenced correctly, your build may decide to take an unplanned vacation.

JAR security: A must!

Be super cautious with third-party .jar files. You wouldn't want your project to be the host for a malware party, would you?