如何在处理类文件时解决NoClassDefFoundError

时间:2012-01-11 12:38:48

标签: java import classpath package

我仍在尝试学习Java并决定更进一步,并尝试使用外部类文件来存储数据结构(如C中的头文件)。我一直在运行NoClassDefFoundError,并想要一些方法或技术可以告诉我出错的地方(以及在哪里),因为消息对我来说不是很清楚:(

我的目录结构设置如下:

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

'main'程序编码如下:

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");
        }
    }
}

'数据结构'编码如下:

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;
   }
}

我已经用javac编译了两个.java文件并且正在尝试使用java -classpath的几个变体(也尝试-cp)从'root'运行主类./://Java/FindGlycopeps/glycoproteomics FindGlycopeps,它不断给出错误,如:

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.

有什么好方法可以弄清楚它是如何出错的,以及在我的“思考”中应该改变什么?我希望能够解释为什么应该做的事情,而不是“如果可能的话,这样做来解决它”。

提前致谢

2 个答案:

答案 0 :(得分:4)

Java类文件必须位于与包名称相同的目录中。因此,您需要将~/programming/Java/FindGlycopeps添加到类路径(而不是~/programming/Java/FindGlycopeps/glycoproteomics),文件FindGlycopeps.class必须位于~/programming/Java/FindGlycopeps/glycoproteomics(而不是~/programming/Java/FindGlycopeps

答案 1 :(得分:1)

文件FindGlycopeps.class必须位于glycoproteomics目录中。包结构由文件系统上的目录结构镜像。