0

I have created a java application that i would like to use as an external library. So i create a jar file and import it on a different project. When i use my library i call

notif.addMachineStat(ip,"Harvester");

which is declared as

public void addMachineStat(String ip,String computerTitle) throws InterruptedException, SigarException {
        Resources rs=new Resources(new Sigar());
        addMachineStat(ip,computerTitle,Double.toString(rs.getAllCpus()),Double.toString(rs.getMemory()),rs.getNetwork(),Double.toString(rs.getFreeHddPerc()));
    }

The problem seems to be with the SigarException that is thrown from the second line (addMachineStat(....)). For some reason the SigarException class (import org.hyperic.sigar.SigarException;) is not carried from the jar file to the new project. Would i have to import it on the other project as well? isnt there a way to get it from the jar with out the user having to import it?

Funny thing, if I use Maven on the second project (my library project uses Maven in the first place), it seems to be working just fine.

Skaros Ilias
  • 1,008
  • 12
  • 40

1 Answers1

0

Yes, you have to import all jars that your code and libraries you use depend on unless you use build tool like maven or gradle.
Maven has Transitive Dependencies feature that makes all work

Transitive dependencies are a new feature in Maven 2.0. This allows you to avoid needing to discover and specify the libraries that your own dependencies require, and including them automatically.

This feature is facilitated by reading the project files of your dependencies from the remote repositories specified. In general, all dependencies of those projects are used in your project, as are any that the project inherits from its parents, or from its dependencies, and so on

That is why it works fine.

So you have several options (in descending preference order):

  1. Keep using maven or other build tool. All popular IDEs has integration with build tools (e.g. M2Eclipse plugin)
  2. Assembly your library with all dependincies into one jar as described in Including dependencies in a jar with Maven
  3. Manage dependencies manually
Community
  • 1
  • 1
Evgeny
  • 2,483
  • 1
  • 17
  • 24