gpt4 book ai didi

java - 我的代码有什么问题?线程 “main” java.lang.NoClassDefFoundError : song (wrong name: Song)中的异常

转载 作者:行者123 更新时间:2023-12-02 11:06:56 24 4
gpt4 key购买 nike

我收到此错误,不确定为什么我的程序基本上是读取.txt文件并将其保存到数组吗?任何帮助深表感谢。

error message:Exception in thread "main" java.lang.NoClassDefFoundError: song (wrong name: Song)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

代码:
import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.charset.*;

class Song {
private int songId; // The unique song identifier
private String title; // The song title
private String artist; // The song artist
private String genre; // The genre of the song
private String album; // The album name
private String songData;

public Song(int songId, String title, String artist, String genre,
String album, String songData) {
this.songId = songId;
this.title = title;
this.artist = artist;
this.genre = genre;
this.album = album;
this.songData = songData;
}

public String toString() {
return "Id: " + songId + ", Title: " + title + ", Artist: " + artist +
", Genre: " + genre + ", Album: " + album + ", Data: " + songData;
}
}

class Ideone {
public static void main (String[] args) throws java.lang.Exception {
try {
FileInputStream fstream = new FileInputStream("songCollection.txt");
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;

while ((strLine = br.readLine()) != null) {
String[] splitOut = strLine.split(", ");
for (String token : splitOut)
System.out.println(token);
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}

Readable fileSong;
String[] songData = new Scanner(fileSong);

while (songData.hasNextLine()) {
String songCollection = songData.nextLine();
songData = songCollection.split(",");

List<Song> songs = new ArrayList<>();

try (BufferedReader input = new BufferedReader(new InputStreamReader(
System.in, Charset.forName("UTF-8")))) {
String line;
while ((line = input.readLine()) != null) {
String[] arr = line.split(",");
songs.add(new Song(Integer.parseInt(arr[0]), arr[1], arr[2],
arr[3], arr[4], arr[5]));
}
} catch (IOException e) {
e.printStackTrace();
}

for (Song song : songs) {
System.out.println(song.toString());
}
}
}

最佳答案

发生此问题是因为该文件存储为song.java而不是 Song.java [请注意类名中的大写字母S]

其次,确保您有一个主类,以防您使用命令调用程序

Java歌曲

否则,您可能还会收到以下错误。
线程“主”中的异常java.lang.NoSuchMethodError:主

关于java - 我的代码有什么问题?线程 “main” java.lang.NoClassDefFoundError : song (wrong name: Song)中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19581141/

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