devoured elysium
devoured elysium

Reputation: 105067

Exporting a Jar library in Java -- how to bundle its dependencies so I don't get NoClassDefFound / ClassNotFound errors when using it?

I've developed a library L that made use of some dependency .jar files D. I've then decided to export it as a Jar J, using Eclipse.

When trying to make use of J, I get java.lang.NoClassDefFound / java.lang.ClassNotFound errors.

What's the easiest way to have my library L deployed? I'd prefer to have it packed together with its dependencies, but it's acceptable to have them phisically separated from L.

I've looked up inside the L.jar and the dependencies are definitely there, so it seems like a matter of having a file telling java where to look them up for.

Thanks

Upvotes: 0

Views: 197

Answers (3)

goodsong
goodsong

Reputation: 46

I am using Eclipse Juno Release(4.2), it's very handy to do this.

  1. Run your main class in eclipse to see if your project can run correctly
  2. Right click project name and select export
  3. Select java->Runnable Jar file
  4. Next, select your configuration, if you don't do step 1, you can't select. select a export position and give a file name, e.g. myapp.jar select 'Package required libraries into generated jar'
  5. fininish

run command java -jar myapp.jar

Upvotes: 0

Mark Bramnik
Mark Bramnik

Reputation: 42451

If you want to create and pack the jar along with dependencies you can consider to use some programming for that. Take a look on JBoss ShrinkWrap

Hope this helps

Upvotes: 0

AlexR
AlexR

Reputation: 115328

User maven or gradle. These 2 build tools do exactly what you need.

Upvotes: 1

Related Questions