Ok, so here's what we've got:
We've got two library packages, which we've compiled into jars.
package starwars; public class JarJar { public void JarSayHello() { System.out.println("Jaaaaar!!"); } }
package barwars; public class BarBar { public void BarSayHello() { System.out.println("Baaaaa!!"); } }
We compile these with
javac -d bin -sourcepath src src/barwars/BarBar.java jar cvf barwars.jar -C bin .
and
javac -d bin -sourcepath src src/starwars/JarJar.java jar cvf starwars.jar -C bin .
All nicely into jars for us.
Now we want to include these two jars into another java project.
so we've got
- /project/src/a_pack/HelloWorld.java
- /project/libs/starwars.jar
- /project/libs/barwars.jar
- /project/manifest.txt
package a_pack; import starwars.JarJar; import barwars.BarBar; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); JarJar myJar = new JarJar(); myJar.JarSayHello(); BarBar myBar = new BarBar(); myBar.BarSayHello(); } }
Manifest.txt
Main-Class: a_pack.HelloWorld Class-Path: libs/starwars.jar libs/barwars.jar
Now we compile this with:
javac -d bin -sourcepath src -cp "libs/starwars.jar;libs/*" src/a_pack/HelloWorld.java jar cvfm helloworld.jar manifest.txt -C bin .
And this compiles and runs fine.
Now I have two problems.
Firstly - if I move this jar file to somewhere else, and try run it, then I'll get:
Exception in thread "main" java.lang.NoClassDefFoundError: starwars/JarJar
Now I can fix this by moving the libs folder to wherever I move the jar. But this strikes me as messy (what if there is already a libs folder in that location?).
Ideally what I'd like to do, is include the referenced jars inside the jar, so there's one jar that contains everything that's required to run inside itself.
Is this possible? (And is it good practise?)