我的代码出了什么问题?线程“main”中的异常java.lang.NoClassDefFoundError:song(错误的名字:Song)

时间:2013-10-25 04:33:17

标签: java compiler-errors syntax-error

我收到此错误,不确定为什么我的程序基本上是在读取.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());
        }
    }
}

2 个答案:

答案 0 :(得分:0)

问题正在发生,因为文件存储为song.java而不是 Song.java [注意类名中的Capital S]

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

调用该程序

java歌曲

另外,您可能会收到以下错误。 线程&#34; main&#34;中的例外情况java.lang.NoSuchMethodError:main

答案 1 :(得分:-1)

我认为你的歌曲类会抛出异常,因为构造函数没有完全初始化。

因为你的主要方法类正在提供这些错误