You can use Java to install the external libraries programmatically using Class.getResourceAsStream() and a FileOutputStream. Libraries will usually go in Java\lib\ext\
.
Here's how I approached it:
I placed a copy of all the JAR's I used into a .res
subpackage. From there, I can copy them anywhere.
private void installLibraries() {
new Thread() {
@Override
public void run() {
System.out.println("Checking for libraries");
File jre = new File(System.getProperty("java.home"));
File jar = new File(jre, "lib/ext/JAR_NAME.jar");
//Create more File objects that wrap your JAR's
try {
boolean added = false;
if (!jar.exists()) {
copyResource(jar, "JAR_NAME.jar");
added = true;
}
//Repeat for more JAR's
if (added) {
System.out.println("Libraries installed.");
} else {
System.out.println("Library check complete");
} catch (IOException ex) {
System.out.println("Library installation failed.");
ex.printStackTrace(System.out);
}
}
private void copyResource(File dest, String src) throws IOException {
System.out.println("Copying resource " + src + " to " + dest);
InputStream in = THIS_CLASS_NAME.class.getResourceAsStream("/YOUR_PACKAGE/res/" + src);
dest.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int i = 0;
while ((i = in.read(buffer)) > 0) {
out.write(buffer, 0, i);
}
out.close();
in.close();
}
}.start();
}
Run this method before you do anything else, and you can completely ignore the external JAR files that Netbeans gives you, and just distribute the one. As an additional note, I used this method to install javax.comm
, which didn't like being distributed externally. It came with a .dll file and a properties file. These files can be installed using the exact same method, but it's worth noting that the .dll file must be placed in the Java\lib\
directory and properties files go in the Java\lib\
directory (not in the \ext
folder).