Click to See Complete Forum and Search --> : Basic question
mcmahoge
March 13th, 2005, 12:36 PM
Hi there everybody,
when I compile my programs there's no problem, but when I run them a message come up on the screen, instead of the program. The message reads ' Exception in thread "main" java.lang.NoClassDefinitionFoundError: Two methods
Press any key to continue
Two methods here is the name of the program,
any suggestions?
Thanks a million,
George
svenhag
March 13th, 2005, 12:46 PM
Can you post the code?
mcmahoge
March 13th, 2005, 12:54 PM
Sure thing, here it is
import java.io.*;
class Methods
{
public static int square(int x)
//Takes an integer x as an argument
{
return x*x; //returns the result as an integer
}
public static void display(int a)
//Takes an integer and displays it
{
System.out.println("The answer is: " + a);
}
public static void main(String[] args)
{
int number=4; //initialises the number to 4
int answer=square(number);
//calls the method square to deliver a value
display(answer); // Displays the value found in answer
}
}
hope this helps,
thanks again
Joe Nellis
March 13th, 2005, 01:16 PM
That error usually means you are not in the right directory when executing java.exe or you have not prepended the package name or the name you have given is wrong. "Two methods" is not the name of the program. "Methods" is the name. Are you trying to rename the compiled .class files for some reason?
Assuming your PATH environment is set and the Methods.class file resides in somedirectory this should work.
C:\somedirectory\java Methods
Also, in the future, please use the CODE tags when posting code in the forums, it retains the formatting of the code so that others can read it.
mcmahoge
March 13th, 2005, 01:25 PM
Thanks a mil, as you had said it was the name of the program that had been misleading me,
Sorry about the code tags thing, in future I will,
thanks again
George
Hobson
March 13th, 2005, 01:35 PM
Most common reasons for this exception are:
1. Your main project file has to be named EXACTLY like public class in this file, and its CASE SENSITIVE, so if your class is called Methods, file must be called Methods.class. You cannot rename this file.
2. When you are invoking your java app, you need to type the name of the main class in command line EXACTLY like it is, its also case-sensitive: c:\somedir>java Methods
Hob
mcmahoge
March 13th, 2005, 01:56 PM
you're so right Hobs, I changed that detail and hey presto! the program works,
thanks a mil,
George
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.