-2

I am trying to create a class to send information on the day of the week. For some reason when I try to include the class I get an error saying:

CreateCalendarVisuals.java:1: error: cannot access FindDateFindTime
import FindDateFindTime.FindDateFindTime;
                       ^
  bad class file: .\FindDateFindTime\FindDateFindTime.class
    class file contains wrong class: FindDateFindTime
    Please remove or make sure it appears in the correct subdirectory of the classpath.

I have creat this code to find the date/time:

import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.LocalTime;

public class FindDateFindTime
{
// public static void main(String[] args)
// {
//         LocalDate date = LocalDate.now();
//         LocalTime time = LocalTime.now();
//
//         System.out.println("Date: " + date + "; Time: " + time);
// }

public int dayOfWeek(int year, int month, int day)
{
        LocalDate date = LocalDate.of(year, month, day);

        DayOfWeek d = date.getDayOfWeek();
        return d.getValue();
}
}

the class is public so I don't know why it would say that it cannot access. I am trying to call that class with this code:

import FindDateFindTime.FindDateFindTime;

public class CreateCalendarVisuals
{
public static void main(String[] args)
{
        FindDateFindTime d = new FindDateFindTime();
         System.out.println(d.dayOfWeek(2021, 04, 24));
}
}

My FindDateFindTime class is in a folder with the same name so I am that is why I have "import FindDateFindTime.FindDateFindTime". I have tried to look this up but I can't seem to find any solutions. I have also tried to do this with a package but it gives me the same issue. Could someone please help, I honestly don't know what to do.

1
  • 1
    Does FindDateFindTime use a package of the same name? If not, then the import should be the more simple: import FindDateFindTime; Commented Apr 25, 2021 at 15:25

2 Answers 2

0

Your class CreateCalendarVisuals imports the FindDateFindTime class as FindDateFindTime.FindDateFindTime; this means that your class FindDateFindTime belongs to the package FindDateFindTime so the first line of your class FindDateFindTime has to be package FindDateFindTime. Note that according to the package naming conventions your package's name should be rewritten as finddatefindtime in a finddatefindtime named directory, so your FindDateFindTime class declaration should be:

package finddatefindtime;

public class FindDateFindTime { ... }

And your CreateCalendarVisuals class:

import finddatefindtime.FindDateFindTime;

public class CreateCalendarVisuals { ... }
Sign up to request clarification or add additional context in comments.

6 Comments

I have tried adding the package as you said and in the command prompt, I did "javac -d . FindDateFindTime.java" which made a folder appear with the .class file. Now I get an error saying Error: Could not find or load main class FindDateFindTime Caused by: java.lang.NoClassDefFoundError: finddatefindtime/FindDateFindTime (wrong name: FindDateFindTime) And also in the CreatCalendarVisuals, i get this error, CreateCalendarVisuals.java:1: error: package finddatefindtime does not exist import finddatefindtime.FindDateFindTime;
@KlutzyMender, if you have a folder containing CreateCalendarVisuals.java and a subfolder in the same directory containing FindDateFindTime.java , you have to execute javac CreateCalendarVisuals.java to compile both files.
Ok, i am very new to this so I am so confused, even if I execute the CreateCalendarVisuals I still get this error because I don't know how to create a package.
CreateCalendarVisuals.java:1: error: package finddatefindtime does not exist import finddatefindtime.FindDateFindTime; ^ CreateCalendarVisuals.java:7: error: cannot find symbol FindDateFindTime d = new FindDateFindTime(); ^ symbol: class FindDateFindTime location: class CreateCalendarVisuals CreateCalendarVisuals.java:7: error: cannot find symbol FindDateFindTime d = new FindDateFindTime(); ^ symbol: class FindDateFindTime location: class CreateCalendarVisuals 3 errors
@KlutzyMender You have to create a directory containing CreateCalendarVisuals.java and a subdirectory called finddatefindtime containing FindDateFindTime.java.
|
0

you dont need to import the class if you are using the same package if you are using eclipse and netbeans.{output of the first program no necessary to import the class if it is in the same package

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.