Solved

Java Class Names Causing Error

Posted on 2003-12-07
19
348 Views
Last Modified: 2013-11-23
ive got a weird error with java.
when i name a class say;
TestCustomerDao

i get the error
exception in thread "main" java.lang.nosuchmethoderror: main

a snippet of code below works and runs such as;
===================

package web.proj3.TestClasses;

public class Test
{
      public static void main(String args[])
      {
      System.out.println("testing");
      }
}
===========================

but the following idential apart from class name wont;

package web.proj3.TestClasses;

public class TestCustomerDao
{
      public static void main(String args[])
      {
      System.out.println("testing");
      }
}

===========================

im using Textpad to run my java..it works ok on other apps ive done
the 2 files are run from;
C:\jwsdp-1.3\webapps\web\proj3

is it because the is a limit to the size of a class name?
i have win xp
i have the Java web devolpers pack isntalled and
JSDK 1.4.1

what is the problem?
0
Comment
Question by:gerrymcd
  • 10
  • 5
  • 4
19 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9893102
Class name must be the same as the file name.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9893105
Make sure there isn;t another TestCustomerDao in the classpath
0
 
LVL 92

Expert Comment

by:objects
ID: 9893106
sorry misunderstood your question. Your class is in a package, you should run it with:

java web.proj3.TestClasses.TestCustomerDao
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 92

Expert Comment

by:objects
ID: 9893110
you class should also be in the directory:

C:\jwsdp-1.3\webapps\web\proj3\TestClasses

and add C:\jwsdp-1.3\webapps to your classpath
0
 
LVL 2

Author Comment

by:gerrymcd
ID: 9893159
the above doest really help
as it works at college and works when the class is called Test
anything longer than Test causes prblems such as;

CustomerByNameCommand

so it cud be a class path problem?
or Textpad problem?

what environment variables in XP do i need to set up?
0
 
LVL 92

Expert Comment

by:objects
ID: 9893175
> so it cud be a class path problem?

Your classpath needs to include the directory C:\jwsdp-1.3\webapps
0
 
LVL 92

Expert Comment

by:objects
ID: 9893179
Are the class files in directory:
C:\jwsdp-1.3\webapps\web\proj3\TestClasses

0
 
LVL 2

Author Comment

by:gerrymcd
ID: 9893202
the current class is
but up one dir is where the files are imported
0
 
LVL 92

Expert Comment

by:objects
ID: 9893223
> but up one dir is where the files are imported

imported by what?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9893275
How many TestCustomerDao do you have on your system gerrymod?
0
 
LVL 2

Author Comment

by:gerrymcd
ID: 9896295
the number of TestCustomerDao is not the problem as i even with a new class name longer than Test it still ocuurs
take the below code snippet which gives the same error; but the code is correct and the import statements

===========================================================================
//      CustomerByNameCommand.java

/*
6 - dec - 03
note to myself
proble with this class
it doesnt work because the class name is too long?
the code is fine
*/

package web.proj3.command ;

import java.util.* ;
import java.sql.* ;
import java.io.* ;


/*
CustomerByNameCommand
Q4 (ii)
*/

import javax.servlet.* ;
import javax.servlet.http.* ;

import web.proj3.business.* ;
import web.proj3.dao.* ;
import web.proj3.exceptions.* ;



public class CustomerByNameCommand implements Command
{
      private HttpServletRequest request ;
      private HttpServletResponse response ;
      private HttpSession session ;


      public CustomerByNameCommand(HttpServletRequest aRequest,
                                                      HttpServletResponse aResponse,
                                                      HttpSession aSession)
      {
            request = aRequest ;
            response = aResponse ;
            session = aSession ;
      }

      public String execute()
      {
            String page = null ;
            CustomerDao dao = new CustomerDao() ;
            try
            {
                  //has to be changed
                  String customerName = request.getParameter("customerName") ;
                  ArrayList customers = dao.getCustomersByName(customerName);

                  /*String customerName = request.getParameter("customerName") ;
                  ArrayList customers = dao.getCustomersByName(customerName) ;
                  if (customers.size() == 0)
                  {
                        request.setAttribute("errorMessage", "No customers with this name") ;
                        page = errorPage ;
                  }
                  else
                  {
                        request.setAttribute("customers", customers) ;
                        page = "/" + request.getParameter("command") + ".jsp" ;      // i.e. /CustomersByName.jsp
                  }*/

                  if (customers.size() == 0)
                  {
                        request.setAttribute("errorMessage", NO_CUSTOMERS) ;
                        page = errorPage ;
                  }
                  else
                  {
                        request.setAttribute("customers", customers) ;
                        page = "/" + request.getParameter("command") + ".jsp" ;      // i.e. /Customers.jsp
                  }

            }
            catch(DaoException e)
            {
                  page = errorPage ;
                  request.setAttribute("errorMessage", e.getMessage() ) ;
            }
            return page ;
      }

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9896345
>>
take the below code snippet which gives the same error

This was the error before apparently:

>>
i get the error
exception in thread "main" java.lang.nosuchmethoderror: main
>>

The class you just posted has no main method. You are trying to execute something as an application that is not an application
0
 
LVL 2

Author Comment

by:gerrymcd
ID: 9899119
yes your correct..the example i posted was very good
even if there was a main method it wud still give the error.

but i think i fixed it by adding a Dot to the classpath
0
 
LVL 92

Expert Comment

by:objects
ID: 9899606
> but i think i fixed it by adding a Dot to the classpath

That says to search the current directory for classes, and will work if you are in the correct directory to find the class. Alternatively you could add the relevant directory that contains your package hierarchy which would allow you to run it from anywhere.
0
 
LVL 92

Expert Comment

by:objects
ID: 9899613
Here's some useful details about classpath:
http://mindprod.com/jgloss/classpath.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9900191
>>exception in thread "main" java.lang.nosuchmethoderror: main

is not a classpath error. It's an error caused by trying to run a non-application as an application. This is what happens on my system when I comment out the main method of an application:

>>[protean@localhost dumpit]$ java NoMain

Exception in thread "main" java.lang.NoSuchMethodError: main
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9900262
> It's an error caused by trying to run a non-application as an application

yes but the same error was occurring whether or not the main existed. Thus it was most probably an issue related to the classpath, and another class being executed, or the wrong package name used.

0
 
LVL 92

Expert Comment

by:objects
ID: 9906918
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9906964
>>Thus it was most probably an issue related to the classpath, and another class being executed

which is why i asked how many copies of class X there were on the system and i don't believe i got the answer > 1
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 54
SHA2 certs for IIS AND Java? 2 82
Configure a Bean in an XML file 4 31
servlet example 17 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question