I am still trying to learn Java and decided to take it 1 step further and attempt to use external class files to store data structures (like the header files in C). I keep running into NoClassDefFoundError and would like some methods or techniques that can show me what is going wrong (and where) as the message is not very clear to me 🙁
I have my directory structure set up as follows:
tarskin@5-PARA-11-0120:~/programming/Java/FindGlycopeps$ ls
Default.mzML FindGlycopeps.class FindGlycopeps.java glycoproteomics
tarskin@5-PARA-11-0120:~/programming/Java/FindGlycopeps$ ls glycoproteomics/
Spectrum.class Spectrum.java
The ‘main’ program is coded as follows:
package glycoproteomics;
import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.lang.*;
public class FindGlycopeps {
public static LinkedList outside_tag_reader (String filename, String tag) {
LinkedList<String> someList = new LinkedList<String>();
DataInputStream in = null;
try {
FileInputStream input = new FileInputStream(filename);
in = new DataInputStream(input);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
String regexp = "<"+tag+">(.*)</"+tag+">";
Pattern pattern = Pattern.compile(regexp);
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String buffer = matcher.group(1);
someList.add(buffer);
}
}
in.close();
} catch (Exception x) {
System.err.println(x);
}
return(someList);
}
public static void main (String [ ] args) {
LinkedList<String> binary = new LinkedList<String>();
ArrayList<Spectrum> spectra = new ArrayList<Spectrum>();
String file = args[0];
if (file != null) {
binary = FindGlycopeps.outside_tag_reader(file,"binary");
int j = 0;
for (int i = 0; i < binary.size(); i++) {
String buffer = binary.get(i);
if (i % 2 == 0) {
spectra.add(j, new Spectrum());
spectra.get(j).mzList.add(buffer);
} else {
spectra.get(j).intList.add(buffer);
}
j++;
}
System.out.println(spectra.get(1).mzList); // purely for testing
} else {
System.out.println("No file was specified");
}
}
}
The ‘Data structure’ is coded as follows:
package glycoproteomics;
import java.util.ArrayList;
public class Spectrum implements Comparable<Spectrum>{
float precursor;
ArrayList<String> mzList;
ArrayList<String> intList;
public int compareTo(Spectrum arg0) {
return 0;
}
}
I have compiled both .java files with just javac and been attemping to run the main class, from the ‘root’ using several variations of java -classpath (also tried -cp) ./://Java/FindGlycopeps/glycoproteomics FindGlycopeps and it keeps giving errors such as:
Exception in thread "main" java.lang.NoClassDefFoundError: FindGlycopeps (wrong name: glycoproteomics/FindGlycopeps)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: FindGlycopeps. Program will exit.
What would be good ways to figure out how it is going wrong and what should be changed in my ‘thinking’? I would appreciate an explanation why something should be done instead of ‘do this to fix it’ if possible.
Thanks in advance
Java class files must be in a directory with the same name as the package. So you need to add
~/programming/Java/FindGlycopepsto your classpath (and not~/programming/Java/FindGlycopeps/glycoproteomics) and the fileFindGlycopeps.classmust be in~/programming/Java/FindGlycopeps/glycoproteomics(instead of~/programming/Java/FindGlycopeps)