java - ClassNotFoundException even though the class is properly defined (I think) -
there similar question posted here answer seems have never been found. have tried clean , still getting classnotfoundexception
.
so happening, in linked question, call add function in arraylist<player>
, proceeds tell me cannot find player class, though there , have imported correctly (since can make use of other class in path. try , post code think necessary.
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 * scores haschanged > 0"); writeresultset(resultset); //resethaschanged(); } catch (sqlexception exc) { throw exc; } { 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 { (int = 0; < 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 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 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 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 (int = 0; < configuration.getlistlength(); i++){ if (namelabel[i].gettext() == ""){ //print empty line constraint = autoconstraint.buildconstraint(new autoconstraint(1, + 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, + 2, 1, 1, 20, 1, gridbagconstraints.east)); mainlayout.setconstraints(changelabel[i], constraint); add(changelabel[i]); //arrowlabel constraint = autoconstraint.buildconstraint(new autoconstraint(1, + 2, 1, 1, 5, 1, gridbagconstraints.west)); mainlayout.setconstraints(arrowlabel[i], constraint); add(arrowlabel[i]); //positionlabel constraint = autoconstraint.buildconstraint(new autoconstraint(2, + 2, 1, 1, 5, 1, gridbagconstraints.east)); mainlayout.setconstraints(positionlabel[i], constraint); add(positionlabel[i]); //namelabel constraint = autoconstraint.buildconstraint(new autoconstraint(3, + 2, 1, 1, 40, 1, gridbagconstraints.west)); mainlayout.setconstraints(namelabel[i], constraint); add(namelabel[i]); //scorelabel constraint = autoconstraint.buildconstraint(new autoconstraint(4, + 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 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 support.", "error", joptionpane.error_message); runtime.getruntime().exit(error); } int change = 0; (int = 0; < 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() - - 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 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 scores arraylist scores.add(new player(id, name, score, 0)); } public static void updatescore(int index, int score){//updates score value, not rearrange or assign lastposition or position scores.get(index).setscore(score); } public static int getlength(){//returns current length of scoreboard return scores.size(); } public static int getid(int index){//takes arraylist index value , returns 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(); } }
i hope enough informmation spot error. appreciated. including proper coding habits, new java.
you use eclipse. kind of vague, know advices: right click on project in project explorer, click properties (bottom of context menu). in upcoming properties dialog's left-side tree-view select "java build path", friend. can see libraries included, library , project dependencies. can set order of export , imports, check sources included, etc.
also can check "java compiler" tree node in properties dialog. can see , change java compatibility level, jdk provides jvm you, other settings can important too.
the main cause not found classes not in classpath. since comes own source code, check source folders included in dialog guided on ("source" tab).
check project has desired layout, packages in place. project imported somewhere else?
Comments
Post a Comment