I'm a seasoned programmed and I've come across this very strange problem that I haven't been able to fix so far. I'm getting this error:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: rec/MiscIO
at rec.RECTool.run(RECTool.java:264)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: rec.MiscIO
at java.net.URLClassLoaderrun(Unknown Source)
at java.net.URLClassLoaderrun(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Here is the relevant line:
File savedTo = MiscIO.copyJar(root);
What I dont understand is that this is a small application, and the classes in use are in the same package. Basically identically setup besides the class names and contents. However I specifically get the error with only some of my classes, not all. I use these lines before the error occours, and these are in the same package and everything.
Logger.log("Found new root: " + newRoots[i0]);
...
FileLocker locker = new FileLocker();
...
locker.lock(PathManager.getJar());
So three classes that are basically identical to the setup of the one with the error, and only one throws the exception. This makes no sense to me.
I have tried various arguments to run the jar including classpath modifications and calling the main class directly instead of using the jar command, to no avail, such as these:
java -jar <jarname>
java -cp . <jarname>
java -cp <jarname> -jar <jarname>
java -cp <jarname> <main class here>
However if I unpack the jar and run the program, the error doesn't occur. I thought it might be some dependency of MiscIO failing to load or the static intializer failing to load, but both are absent in my code.
Also, I'm using Netbeans to generate the jar, and the manifest is set correctly. Here's the entire code:
package rec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.URL;
/**
* @author Colby
*/
public class MiscIO {
public static void copyJar(RandomAccessFile out) throws IOException {
URL jarSource = RECTool.class.getProtectionDomain().getCodeSource().getLocation();
InputStream in = null;
try {
in = jarSource.openStream();
copyToRAF(in, out);
} finally {
if (in != null) {
in.close();
}
}
}
public static File copyJar(File saveJarTo) throws IOException {
URL jarSource = RECTool.class.getProtectionDomain().getCodeSource().getLocation();
saveJarTo = new File(saveJarTo, "classlist.jar");
InputStream in = null;
OutputStream out = null;
try {
in = jarSource.openStream();
out = new FileOutputStream(saveJarTo);
copyStream(in, out);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
return saveJarTo;
}
public static void copyStream(InputStream in, OutputStream out) throws IOException {
int len;
byte[] data = new byte[1024 * 8];
while ((len = in.read(data)) != -1) {
out.write(data, 0, len);
}
out.flush();
}
public static void copyToRAF(InputStream in, RandomAccessFile raf) throws IOException {
raf.seek(0L);
int len;
byte[] data = new byte[1024 * 8];
while ((len = in.read(data)) != -1) {
raf.write(data, 0, len);
}
}
public static void copyFile(File from, File to) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
copyStream(in, out);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
On a last note, I thought it might be conflicting versions of Java installed in my machine, or x86/x64 conflict by some fluke, so I uninstalled all Java distributions and installed only the latest of one architecture and the problem remains. Any of you guys seen something like this before?