Javaでカレンダーを作っているのですがエラーが出ました。
エラー内容は
Exception in thread "main" java.lang.NoClassDefFoundError: CalendarCell
at CalendarSystem.<init>(CalendarSystem.java:17)
at CalendarSystem.main(CalendarSystem.java:44)
Caused by: java.lang.ClassNotFoundException: CalendarCell
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
と出てきました。
明らかにCalendarCellというクラスがおかしいのですが修正の仕方がわかりません。
修正に必要と思われるコードは
CalendarSystem
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalendarSystem extends JFrame implements WindowListener{ private CalendarPanel cp; public CalendarSystem(){ super("Calendar System SP6"); setSize(600,500); setLocation(100,100); addWindowListener(this); cp=new CalendarPanel(); WeekPanel wp=new WeekPanel(); ControlPanel cop=new ControlPanel(cp,this); cp.setControlPanel(cop); cp.setYearMonth(2020,6); JPanel p=new JPanel(); p.setLayout(new BorderLayout()); p.add(wp,BorderLayout.NORTH); p.add(cp); add(p); add(cop,BorderLayout.NORTH); } public void windowOpened(WindowEvent ev){} public void windowClosing(WindowEvent ev){ cp.saveSchedules(); dispose(); } public void windowClosed(WindowEvent ev){} public void windowActivated(WindowEvent ev){} public void windowDeactivated(WindowEvent ev){} public void windowIconified(WindowEvent ev){} public void windowDeiconified(WindowEvent ev){} public static void main(String[] args){ (new CalendarSystem()).setVisible(true); } }
CalendarCell
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class CalendarCell extends JPanel{ private Calendar date; private JPanel panel; public CalendarCell(){ setBackground(Color.white); setLayout(new FlowLayout(FlowLayout.LEFT)); panel=new JPanel(); panel.setLayout(new GridLayout(0,1)); setLayout(new BorderLayout()); add(panel,BorderLayout.NORTH); } public void setDate(int y,int m,int d){ date=Calendar.getInstance(); date.set(y,m-1,d); int dow=date.get(Calendar.DAY_OF_WEEK); JLabel l=new JLabel(""+d); if(dow==Calendar.SUNDAY) l.setForeground(Color.red); if(dow==Calendar.SATURDAY) l.setForeground(Color.blue); panel.add(l); /*D ラベルを置く*/ } public void clear(){ panel.removeAll(); } public void addSchedule(Schedule sc){ panel.add(ScheduleLabel(schedule)); /*E パネルに追加*/ } }
CalendarPanel
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*; import java.text.*; public class CalendarPanel extends JPanel{ private CalendarCell[][] cc; private ControlPanel cop; private int current_year,current_month; private ArrayList<Schedule> sc_list; public CalendarPanel(){ setLayout(new GridLayout(6,7,1,1)); cc=new CalendarCell[6][7]; for(int i=0;i<6;i++) for(int j=0;j<7;j++) add(cc[i][j]=new CalendarCell()); sc_list=new ArrayList<Schedule>(); loadSchedules(); /*ファイルから予定を読む?*/ } public void setControlPanel(ControlPanel cop){ this.cop=cop; } public void setYearMonth(int y,int m){ Calendar cal=Calendar.getInstance(); cal.set(y,m-1,1); int dow=cal.get(Calendar.DAY_OF_WEEK); int d_max=cal.getActualMaximum(Calendar.DATE); int d=1; clearAllCells(); for(int i=0;i<6;i++){ for(int j=0;j<7;j++){ if(i==0 && Calendar.SUNDAY+j<dow) continue; if(d>d_max) continue; cc[i][j].setDate(y,m,d); d++; } } current_year=y; current_month=m; addAllSchedulesToCell(); validate(); repaint(); } public void clearAllCells(){ for(int i=0;i<6;i++) for(int j=0;j<7;j++) cc[i][j].clear(); /*C removeAll?*/ } public void setYear(int y){ setYearMonth(y,current_month); } public void setMonth(int m){ setYearMonth(current_year,m); } public void nextyear(){ setYearMonth(++current_year,current_month); } public void prevYear(){ setYearMonth(--current_year,current_month); } public void nextMonth(){ if(++current_month>12){ current_year++; current_month=1; } setYearMonth(current_year,current_month); } public void prevMonth(){ if(--current_month<1){ current_year--; current_month=12; } setYearMonth(current_year,current_month); } private CalendarCell getCell(Schedule sc){ int m=sc.getMonth(); int d=sc.getDate(); if(m!=current_month) return null; Calendar cal=Calendar.getInstance(); cal.set(current_year,m-1,1); int dow=cal.get(Calendar.DAY_OF_WEEK); int d_max=cal.getActualMaximum(Calendar.DATE); int p=dow+d-Calendar.SUNDAY-1; int i=p/7; int j=p%7; return cc[i][j]; } private void addAllSchedulesToCell(){ for(Schedule sc:sc_list){ CalendarCell cc=getCell(sc); if(cc==null) continue; cc.addSchedule(sc); } } public void addSchedule(Schedule sc){ sc_list.add(sc); CalendarCell cc=getCell(sc); if(cc!=null) cc.addSchedule(sc); } private String file_name="schedules.csv"; public void saveSchedules(){ try{ PrintWriter pw=new PrintWriter(new FileWriter(file_name)); pw.println(sc_list.size()); for(Schedule sc:sc_list){ pw.println(sc.toCSV()); } pw.close(); }catch(IOException e){ System.err.println(e); } } public void loadSchedules(){ try{ sc_list.clear(); BufferedReader br=new BufferedReader(new FileReader(file_name)); int n=Integer.parseInt(br.readLine()); for(int i=0;i<n;i++){ Schedule sc=new Schedule(br.readLine()); sc_list.add(sc); } br.close(); }catch(IOException e){ System.err.println(e); }catch(ParseException e){ System.err.println(e); } } }
ScheduleLabel
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ScheduleLabel extends JLabel{ private Schedule sc; public ScheduleLabel(Schedule s){ sc=s; setText(sc.toShortString()); setBackground(Color.white); setOpaque(true); } }
Schedule
import java.text.*; import java.util.*; public class Schedule{ private String title; private Date date; private boolean allday; private String place; private String memo; public Schedule(String ti,String dt,String tm,boolean ad, String pl,String dl) throws ParseException { title=ti; SimpleDateFormat sdf; sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm"); date=sdf.parse(dt+" "+tm); allday=ad; place=pl; memo=dl; } public Schedule(String csv) throws ParseException { fromCSV(csv); } public int getYear(){ Calendar cl=Calendar.getInstance(); cl.setTime(date); return cl.get(Calendar.YEAR); } public int getMonth(){ Calendar cl=Calendar.getInstance(); cl.setTime(date); return cl.get(Calendar.MONTH)+1; } public int getDate(){ Calendar cl=Calendar.getInstance(); cl.setTime(date); return cl.get(Calendar.DATE); } public void fromCSV(String csv) throws ParseException { String[] tok=csv.split(","); title=tok[0]; SimpleDateFormat sdf; sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm"); date=sdf.parse(tok[1]); allday=tok[2].equals("1"); place=tok[3]; memo=tok[4].replace("<<NEWLINE>>","\n"); } public String toCSV(){ String memo2=memo.replace("\n","<<NEWLINE>>"); SimpleDateFormat sdf; sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm"); return title+"," +sdf.format(date)+"," +(allday?"1":"0")+"," +place+"," +memo2; } public String toString(){ return "名称:"+title+"\n"+ "日時: "+date+"\n"+ "終日: "+(allday?"○":"×")+"\n"+ "場所: "+place+"\n"+ "メモ: "+memo+"\n"; } public String toShortString(){ if(allday) return title; SimpleDateFormat sdf; sdf=new SimpleDateFormat("HH:mm"); return sdf.format(date)+" "+title; } }
解決していただけましたら宜しくお願いいたします。
追記
<IT技術の処方箋:NoClassDefFoundErrorへの対処>
C:\java>java HelloWorld
Exception in thread
"main" java.lang.NoClassDefFoundError: HelloWorld
エラーの原因として、考えられる理由は二つです。
まずは、HelloWorldのスペルはあっていますか?
HelloWorld.classというファイルが本当に存在しますか?
次に考えられるのが、クラスパスです。
クラスパスの詳細は省きますが、
かりに、
C:\java>java -classpath c:\java HelloWorld
Hello World!
として、うまくいくようなら、間違いなくクラスパスの問題です。
クラスパスとは、HelloWorld.classファイルがどこのディレクトリに存在するかを教える仕組みです。
上の例でいうと、-classpath c:\java とすることで、
c:\javaディレクトリに、HelloWorld.classファイルが
存在しますよ、と明示的に示しています。
ちなみに、↓でも同じ結果になります。
C:\java>java -classpath . HelloWorld
Hello World!
こちらの場合は、-classpath . が、現在(カレント)の
ディレクトリを表します。
あと他の理由としては、
packageを無視していることも考えられます。
例えば、
C:\workspace\test\bin\info\searchman\HelloWorld.class
があって、
HelloWorldのpackageが
info.searchmanとするなら、
以下のような感じで実行できるはずです。
cd c:\workspace\test\bin
C:\workspace\test\bin>java info/searchman/HelloWorld
あなたの回答
tips
プレビュー