I created the following package AccBal.

package AccBal;

public class AccountBalance
{
	private String Name;
	private double Balance;
	public void setData(String name, double balance)
	{
		Name=name;
		Balance=balance;
	}
	public void displayBalance()
	{
		System.out.println("The Account Balance of " + Name + " is $"+Balance);
	}
}

Then I created a class to use this package:

import AccBal.*;
import java.util.Scanner;
import java.io.Console;

class Bank
{
	public static void main()
	{
		byte n;
		//char []tempname;
		String name;
		double Balance;
		
		Scanner input=new Scanner(System.in);
		Console cin=System.console();
		
		System.out.print("Enter the number of Customers: ");
		n=input.nextByte();
		AccountBalance [] acc=new AccountBalance[n];
		for(byte i =0;i<n;i++)
		{
			System.out.println("\nEnter the Name and Account Balance of customer " + (i+1) + ":");
			name=cin.readLine();
			//name=new String(tempname);
			Balance=input.nextDouble();
			acc[i].setData(name,Balance);
		}
		System.out.println("\nThe Details of the Customers are: ");
		for(byte i =0;i<n;i++)
		{
			acc[i].displayBalance();
			
		}
	}
}

My Directory Structure is as follows:

[B]Main Directory:[/B] 
Name : [B]Bank[/B]
Contents: Bank.class, Bank.java, AccBal (subDir)

[B]AccBal Contents:[/B]
AccountBalance.class

I use JDK 1.6 and JRE 1.5 "with no IDE" I use the cmd pmt to compile and run the code.

When I compile it I get no error, but when I run the bytecode, I get following exception as an error:

E:\JAVA Programs\Bank>java Bank
Exception in thread "main" java.lang.NoSuchMethodError: main

Please Help me!

Recommended Answers

All 6 Replies

The main method that the Java command looks for has some args: String[]
you left it off your main() definition.

Is that necessary? As far as I know, its optional to include parameters to receive command line arguments. Correct me if Im wrong.

The signature of the main method must match EXACTLY what the java program is looking for.

its optional to include parameters to receive command line arguments

You do NOT have to pass arguments on the command line.

The command line arguments are optional, and in fact they are seldom used. The String[] parameter is not optional.

Java is not looking for a method called main() - you could have any number of methods called main, and as long as they all take different parameters, that's fine. What Java looks for is a method with the signature main(String[]) - that is, a method called main that takes an array of String as its sole parameter.

Is that necessary? As far as I know, its optional to include parameters to receive command line arguments. Correct me if Im wrong.

indeed it is, but you need to provide the possibility to pass on parameters.
you should consider the main method a method that should only be able to accept String objects as arguments, and that everyone can 'override' this the way they want.

so, where as, user 1 only passes a username, the second user might want to pass a username and a password.
this might explain why you have to put a String array (and, indeed, you don't say anything about the number of elements)

why you can't just omit it: it's been written this way in the original java specs.
consider it to be an implementation of an interface, only the compiler won't test whether it's present or not.

it's as if someone writes the next method:

...

public static String getErrorMessage(ToDo toDo){
  if ( canPerformToDo(toDo) )
    return "";
  return "Could not perform this action";
}
...

and, let's assume that canPerformToDo always returns either 'true' or 'false'.
you would get in about the same situation if you called the 'getErrorMessage' method while passing an ArrayList as a parameter.

it doesn't exist, so it shouldn't be found.

you propably already understood it by the last two posts, but I guess there's no harm in providing an other way of explaining (and yeah, it's 0.09 am and I'm bored, just shoot me :p )

commented: Always worth telling it another way. One of them will get it, I'm sure! +11

Yeah thank you! Ive totally understood it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.