Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Compiling issues in the terminal?

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Compiling issues in the terminal?

    public class Site {
     
    private int siteNumber;
    private String reserved;
    private String Name;
    private String licensePlate;
    private String postalCode;
    private int partyNumber;
     
    public Site(int site, String res, String nam, String lic, String post, int party) {
     
    site = siteNumber;
    res = reserved;
    Name = nam;
    lic = licensePlate;
    post = postalCode;
    party = partyNumber;
    }
     
    public int getSite() {
    return siteNumber;
    }
     
    }
     
    public class Campsite {
       public static void main (String[] args) {
     
    Site site1 = new Site(5, "Reserved", "Caroline Strickland", "A13 F4G", "A1M 3A1", 5);
     
    System.out.println("it is" + site1.getSite());
     
    }
     
    }
     
    //site number (1 - 50)
    //is this a serviced or non-serviced site?
    //is the site reserved?
    //name of camper (last name, followed by first name)
    //license plate number (6 characters)
    //postal code
    //number in party

    With this code, when I compile it under that name Campsite.java, I always get one error saying:

    Campsite.java:1: class Site is public, should be declared in a file named Site.java
    public class Site"

    I do have a file called Site.class in my files though, so I'm wondering what's going wrong?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Compiling issues in the terminal?

    The error message is very specific. There can only be one top-level public class in each source file. That class should also contain the main() method. Remove the 'public' from the Site class declaration and all should be well.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Compiling issues in the terminal?

    Hello.
    It is not necessary or compulsory to have a main() in the top-level public class. It is optional.
    main() is compulsory for the class that we want to run at the command prompt. This class could be top-level or inner. It could be public or non-public. But of course the signature of main() must be public static void main(String[]).
    The following two pieces of code illustrate this:

    // Test.java (contains Test class and Run class)
    public class Test {
      public void doDisplay()  {
          System.out.println("doDisplay() of Test class");
      }
    }
     
    class Run  {
        public static void main(String s[])  {
            Test test=new Test();
            System.out.println("in main() of Run");
            test.doDisplay();
        }
    }
    In the above code, I can type,
    > java Run

    // Test1.java
    public class Test1 {
        public static void doPrint()  {
            System.out.println("doPrint() of Test1 class");
        }
     
         private static class Inner  {
            public static void main(String str[])  {
                System.out.println("in main() of Test1.Inner");
                doPrint();
            }
        }
    }

    In the above code, I can type,
    > java Test1\$Inner

    Thanks,
    Syed.

  4. #4
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Compiling issues in the terminal?

    Ah! Got it all fixed up! Thank you!

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Compiling issues in the terminal?

    Quote Originally Posted by Scorks View Post
    With this code, when I compile it under that name Campsite.java, I always get one error saying:

    Campsite.java:1: class Site is public, should be declared in a file named Site.java
    public class Site"

    I do have a file called Site.class in my files though, so I'm wondering what's going wrong?
    This error was caused because you defined the classes in the same file, and the Campsite was closed before Site started. The solution is to define Site in a new file and not stuck in the bottom of the Campsite's file

Similar Threads

  1. Integrated Java With Terminal
    By MFD-Stark in forum Java Theory & Questions
    Replies: 2
    Last Post: January 2nd, 2013, 12:23 PM
  2. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  3. Terminal Tamagotchi
    By FlurrYx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2011, 01:05 PM
  4. Quick help with using linux terminal
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 0
    Last Post: April 1st, 2011, 09:28 AM
  5. Need some help with OSX Java (Terminal)
    By mkoop in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2011, 12:23 PM

Tags for this Thread