user2884621
user2884621

Reputation: 151

Beginner Java application constructor issue

I have a class called MyDate which is accessible in another project (all the methods show up in a pop up while using an instance of the class). However, when I run the application it seems there is an error when I use the constructor on the instance of MyDate. It makes no difference if I use the default constructor or the one with parameters.

This is the error i get:

Exception in thread "main" java.lang.NoClassDefFoundError: MyClasses/MyDate
    at Lesson2Test.main(Lesson2Test.java:10)

Here is my class file:

package MyClasses;

import java.io.PrintStream;
import java.util.Scanner;

public class MyDate {
private int Month, Day, Year;

public MyDate(){
    Month = 1;
    Day = 1;
    Year = 2000;
}

public MyDate(int m, int d, int y){
    Month = m;
    Day = d;
    Year = y;
}

public int GetMonth(){
    return Month;
}

public int GetDay(){
    return Day;
}

public int GetYear(){
    return Year;
}

public void SetMonth(int m){
    Month = m;
}

public void SetDay(int d){
    Day = d;
}

public void SetYear(int y){
    Year = y;
}

public void Write(PrintStream ps){
    ps.printf("%d\t%d\t%d", Month, Day, Year);
}

public void Read(Scanner s){
    Month = s.nextInt();
    Day = s.nextInt();
    Year = s.nextInt();
}
}

and here is my main class file:

import bin.MyClasses.MyDate;
import bin.MyClasses.MyTime;

public class Lesson2Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    MyDate date1;
    date1 = new MyDate(5, 5, 5);
    date1.Write(System.out);

}

}

Upvotes: 1

Views: 80

Answers (1)

StFS
StFS

Reputation: 1710

Where is that "bin." in your import statements coming from? I think that should just be "import MyClasses.MyDate"

If you're having problems in your IDE when you remove the "bin" from your import statement (your IDE won't autocomplete your class names), then you probably have a classpath issue in your settings. Your classpath is then probably pointing to the directory that contains "bin" but it should be pointing to the "bin" directory itself.

Upvotes: 4

Related Questions