gpt4 book ai didi

java - 使用类文件时如何解决 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 08:09:44 25 4
gpt4 key购买 nike

我仍在尝试学习 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

“主”程序编码如下:

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)./://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.

有什么好方法可以弄清楚它是如何出错的,以及我的“想法”应该改变什么?如果可能的话,我将不胜感激解释为什么应该做某事而不是“这样做来修复它”。

提前致谢

最佳答案

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

关于java - 使用类文件时如何解决 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com