-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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
KlutzyMender
  • 18
  • 1
  • 3

2 Answers2

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 { ... }
dariosicily
  • 4,239
  • 2
  • 11
  • 17
  • 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 Apr 26 '21 at 13:03
  • @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. – dariosicily Apr 26 '21 at 13:31
  • 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. – KlutzyMender Apr 26 '21 at 15:10
  • `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 Apr 26 '21 at 15:10
  • @KlutzyMender You have to create a directory containing CreateCalendarVisuals.java and a subdirectory called finddatefindtime containing FindDateFindTime.java. – dariosicily Apr 26 '21 at 15:19
  • Ok, I was just doing it wrong you are definitely right, thanks it all works now. – KlutzyMender Apr 27 '21 at 21:34
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