Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error message after first compile

    My code compiles correctly everytime, but if i change anything, i must also change the name of class Lab168 to get the program to run. i get the runtime error of "Exception in thread "main" java.lang.NoSuchMethodError: main". why do i have to change the class name each time?

    Code:
    class Lab168 {
    	public static void main (String args[]) throws IOException {
    		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		System.out.print("Enter a string: ");
    		Palindrome p = new Palindrome(input.readLine());
    		p.displayData();
    	}
    }
    
     
    class Palindrome {
    	private String s;
    	private String p;
    	
    	public Palindrome (String x) {
    		s = x;
    	}
    	public void displayData() {
    		System.out.println("string: " + s);		
    	}
    }
    "There is more than one way to do it."

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    What do you mean by "change anything". What is it that you changed? The file name must be the same as your class name. So make sure you name your file Lab168.java and then compile it and you should end up with Lab168.class. The same thing applies to your second class. The source file should be named Palindrome.java and then when you compile it you will end up with Palindrome.class.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep, that's what I noticed as well. Every class needs to be in its own file (except innerclasses of course..), and the file name should be the same (even case I believe) as the class name.

    Sadiq.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •