Question

I have the following simple code :

public class Date
{
 byte day;
 byte month;
 short year;
}
 class DateUse{
  public static void main(String[] args){
   Date date = new Date();
   date.day = 15;
  System.out.println(date.day);
  }
}

I am using Linux terminal to compile my .java code :

javac Date.java

In Windows environment any IDE would compile without any error ,why it is giving me errors in Linux ?

Thank u in advance.

Was it helpful?

Solution

It sounds like you tried to run Date instead of DateUse:

$ javac Date.java

$ java Date
Exception in thread "main" java.lang.NoSuchMethodError: main

$ java DateUse
15

OTHER TIPS

Make DateUse a public class, and make it before the Date class.

Save the file as DateUse.java and compile it with

javac DateUse.java

The DateUse class contains the main method, not Date.

Your main method needs to be in the Date class as your file name represents the class name. Your code should be as follows:

public class Date{
     public byte day;
     public byte month;
     public short year;

     public static void main(String[] args){

       Date date = new Date();
       date.day = 15;
       System.out.println(date.day); 
     }

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top