ClassNotFoundException即使正确定义了类(我认为)

时间:2013-05-13 02:30:04

标签: java eclipse class classnotfoundexception

有一个类似的问题here,但答案似乎从来没有找到过。我已经尝试了干净,但仍然得到了ClassNotFoundException

所以发生了什么,就像链接的问题一样,我在ArrayList<Player>中调用了add函数,它继续告诉我它找不到Player类,即使它在那里并且我已正确导入它(因为它可以使用该路径中的其他类。我将尝试发布尽可能多的代码。

au.thewebeditor.scoreboard.apps.AccessMySQL.java

package au.thewebeditor.scoreboard.apps;

import java.sql.*;

import au.thewebeditor.scoreboard.defs.*;

public class AccessMySQL {
    private static Connection connection = null;
    private static Statement statement = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;

    public static void readData() throws SQLException, ClassNotFoundException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://HOSTNAME/socmedi2_rce?user=socmedi2_sam&password=abc123");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM scores WHERE hasChanged > 0");
            writeResultSet(resultSet);
            //resetHasChanged();
        } catch (SQLException exc) {
            throw exc;
        } finally {
            close();
        }
    }   
    private static void writeResultSet(ResultSet resultSet) throws SQLException {
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int score = resultSet.getInt("score");
            int lastScore = resultSet.getInt("lastScore");
            if (lastScore == 0){
                Scoreboard.addScore(id, name, score);
            } else {
                for (int i = 0; i < Scoreboard.getLength(); i++){
                    if (Scoreboard.getID(i) == id){
                        Scoreboard.updateScore(i, score);
                    }
                }
            }
        }
        Scoreboard.sortScoreboard();
    }
    private static void resetHasChanged() throws SQLException{
        preparedStatement = connection.prepareStatement("UPDATE scoreboard.scores SET hasChanged = 0 WHERE hasChanged > 0");
        preparedStatement.executeUpdate();
    }
    private static void close() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            //DO NOTHING???
        }
    }
}

au.thewebeditor.scoreboard.apps.Scorboard.java

package au.thewebeditor.scoreboard.apps;

import java.awt.*;
import java.awt.geom.*;
import java.sql.SQLException;
import java.util.*;

import javax.swing.*;

import au.thewebeditor.scoreboard.defs.*;

public class Scoreboard extends JWindow {
    static Config configuration = new Config();
    private static ArrayList<Player> scores = new ArrayList<Player>(20);
    private static JLabel titleLabel = new JLabel();
    private static JLabel subtitleLabel = new JLabel();
    private static JLabel[] positionLabel = new JLabel[configuration.getListLength()];
    private static JLabel[] nameLabel = new JLabel[configuration.getListLength()];
    private static JLabel[] scoreLabel = new JLabel[configuration.getListLength()];
    private static JLabel[] changeLabel = new JLabel[configuration.getListLength()];
    private static JLabel[] arrowLabel = new JLabel[configuration.getListLength()];

    public Scoreboard(){
        //Set to full screen
        this.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        updateLabels(); //Set value of headings
        updateScores();
        this.getContentPane().setBackground(Color.white);
        GridBagLayout mainLayout = new GridBagLayout();
        setLayout(mainLayout);//Grid size is 5 across: Position, name, $value, change, arrow
        //TITLE LABEL
        GridBagConstraints constraint = AutoConstraint.buildConstraint(new AutoConstraint(0, 0, 5, 1, 100, 0, GridBagConstraints.CENTER));
        mainLayout.setConstraints(titleLabel, constraint);
        add(titleLabel);
        //SUBTITLE LABEL
        constraint = AutoConstraint.buildConstraint(new AutoConstraint(0, 1, 5, 1, 100, 0, GridBagConstraints.CENTER));
        mainLayout.setConstraints(subtitleLabel, constraint);
        add(subtitleLabel);
        //SCOREBOARD
        for (int i = 0; i < configuration.getListLength(); i++){
            if (nameLabel[i].getText() == ""){
                //Print empty line
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(1, i + 2, 5, 1, 100, 1, GridBagConstraints.WEST));
                mainLayout.setConstraints(nameLabel[i], constraint);
                add(nameLabel[i]);
            } else {
                add(arrowLabel[i]);
                //ChangeLabel
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(0, i + 2, 1, 1, 20, 1, GridBagConstraints.EAST));
                mainLayout.setConstraints(changeLabel[i], constraint);
                add(changeLabel[i]);
                //ArrowLabel
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(1, i + 2, 1, 1, 5, 1, GridBagConstraints.WEST));
                mainLayout.setConstraints(arrowLabel[i], constraint);
                add(arrowLabel[i]);
                //PositionLabel
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(2, i + 2, 1, 1, 5, 1, GridBagConstraints.EAST));
                mainLayout.setConstraints(positionLabel[i], constraint);
                add(positionLabel[i]);
                //NameLabel
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(3, i + 2, 1, 1, 40, 1, GridBagConstraints.WEST));
                mainLayout.setConstraints(nameLabel[i], constraint);
                add(nameLabel[i]);
                //ScoreLabel
                constraint = AutoConstraint.buildConstraint(new AutoConstraint(4, i + 2, 1, 1, 30, 1, GridBagConstraints.WEST));
                mainLayout.setConstraints(scoreLabel[i], constraint);
                add(scoreLabel[i]);
            }
        }
        this.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        setVisible(true);
    }

    public static void updateLabels() {//Updates heading labels
        titleLabel.setText(configuration.getTitle());
        subtitleLabel.setText(configuration.getSubtitle());
        titleLabel.setFont(new Font("Segoe UI", Font.BOLD, (int)(42*configuration.getFontAdjust())));
        subtitleLabel.setFont(new Font("Segoe UI", Font.BOLD, (int)(32*configuration.getFontAdjust())));
    }
    public static void updateScores() {//Updates scoreboard labels
        try {
            AccessMySQL.readData();
        } catch (SQLException e) {
            String[] options = {"Retry", "Exit"};
            int response = JOptionPane.showOptionDialog(null, "MySQL database failed to load. Error code: " + Integer.toString(e.getErrorCode()) + ".\n" + e.getMessage(), "MySQL Error", 0, JOptionPane.ERROR_MESSAGE, null, options, options[1]);
            if (response == 1 || response == JOptionPane.CLOSED_OPTION)
                Runtime.getRuntime().exit(ERROR);
            else
                Program.redrawScoreboard();
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Hard Coded Failure, Contact Mowgli for Support.", "Error", JOptionPane.ERROR_MESSAGE);
            Runtime.getRuntime().exit(ERROR);
        }
        int change = 0;
        for (int i = 0; i < configuration.getListLength(); i++){
            try {
                positionLabel[i] = new JLabel(Integer.toString(i + 1) + ". ");
                positionLabel[i].setFont(new Font("Segoe UI", Font.PLAIN, (int)(24*configuration.getFontAdjust())));
                nameLabel[i] = new JLabel(scores.get(i).getName());
                nameLabel[i].setFont(new Font("Segoe UI", Font.BOLD, (int)(24*configuration.getFontAdjust())));
                scoreLabel[i] = new JLabel("$" + Integer.toString(scores.get(i).getScore()));
                scoreLabel[i].setFont(new Font("Segoe UI", Font.PLAIN, (int)(24*configuration.getFontAdjust())));
                change = scores.get(i).getLastPosition() - i - 1;
                changeLabel[i] = new JLabel();
                changeLabel[i].setFont(new Font("Segoe UI", Font.BOLD, (int)(24*configuration.getFontAdjust())));
                if (scores.get(i).getLastPosition() == 0){
                    changeLabel[i].setText("NEW! ");
                    changeLabel[i].setForeground(Color.yellow);
                    arrowLabel[i] = new JLabel(" " + Character.toString((char)171));
                    arrowLabel[i].setForeground(Color.yellow);
                } else if (change > 0){
                    changeLabel[i].setText("+" + Integer.toString(change) + " ");
                    changeLabel[i].setForeground(Color.green);
                    arrowLabel[i] = new JLabel(" " + Character.toString((char)233));
                    arrowLabel[i].setForeground(Color.green);
                } else if (change < 0){
                    changeLabel[i].setText(Integer.toString(change) + " ");
                    changeLabel[i].setForeground(Color.red);
                    arrowLabel[i] = new JLabel(" " + Character.toString((char)234));
                    arrowLabel[i].setForeground(Color.red);
                } else {
                    changeLabel[i].setText(Character.toString((char)177)+"0 ");
                    changeLabel[i].setForeground(Color.gray);
                    arrowLabel[i] = new JLabel(" " + Character.toString((char)108));
                    arrowLabel[i].setForeground(Color.gray);
                }
                scores.get(i).setLastPosition(i+1); //Sets last position before sort is called.
                arrowLabel[i].setFont(new Font("Wingdings", Font.PLAIN, (int)(24*configuration.getFontAdjust())));
            } catch (IndexOutOfBoundsException e) {
                nameLabel[i] = new JLabel("");
            }
        }
    }
        public static void addScore(int id, String name, int score){//Adds new score to scores ArrayList
            scores.add(new Player(id, name, score, 0));
        }
    public static void updateScore(int index, int score){//Updates score value, does not rearrange or assign lastPosition or position
        scores.get(index).setScore(score);
    }
    public static int getLength(){//Returns the current length of the scoreboard
        return scores.size();
    }
    public static int getID(int index){//takes the ArrayList index value and returns the ID value
        return scores.get(index).getID();
    }
    public static void sortScoreboard(){//Sorts scoreboard DESC
        Collections.sort(scores);
    }
}

au.thewebeditor.scoreboard.apps.Program.java

package au.thewebeditor.scoreboard.apps;

   public class Program {
    private static Scoreboard sb;
    private static ConfigPanel cp;

public Program(){
    sb = new Scoreboard();
    cp = new ConfigPanel();
}

public static void redrawScoreboard() throws NullPointerException{
    try{
        sb.dispose();
    } catch (NullPointerException e){
        //DO NOTHING
    }
    sb = new Scoreboard();
    cp.toFront();
}

public static void showConfig(){
    cp.setVisible(true);
    cp.toFront();
}

public static void main(String[] arguments){
    new Program();
}
}

我希望这足以发现错误信息。任何帮助将不胜感激。包括正确的编码习惯,因为我是Java新手。

1 个答案:

答案 0 :(得分:0)

您使用Eclipse。这有点模糊,但从我们所知道的建议来看: 在Project Explorer中右键单击项目,然后单击Properties(上下文菜单的底部)。在即将出现的Properties对话框的左侧树视图中选择“Java Build Path”,这是你的朋友。您可以看到包含哪些库,库和项目依赖项是什么。您可以设置导出和导入的顺序,检查包含的来源等等。

您还可以在“属性”对话框中检查“Java编译器”树节点。您可以查看和更改Java兼容级别,JDK为您提供JVM,其他设置也很重要。

主要原因可能是未找到的类不在类路径中。由于它来自您自己的源代码,请检查我在(“源”选项卡)上引导您的对话框中包含的源文件夹。

检查您的项目是否具有所需的布局,包已到位。这是您从其他地方导入的项目吗?