gerrymcd
asked on
Java Class Names Causing Error
ive got a weird error with java.
when i name a class say;
TestCustomerDao
i get the error
exception in thread "main" java.lang.nosuchmethoderro r: 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("testin g");
}
}
========================== =
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("testin g");
}
}
========================== =
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\p roj3
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?
when i name a class say;
TestCustomerDao
i get the error
exception in thread "main" java.lang.nosuchmethoderro
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("testin
}
}
==========================
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("testin
}
}
==========================
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\p
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?
Class name must be the same as the file name.
Make sure there isn;t another TestCustomerDao in the classpath
sorry misunderstood your question. Your class is in a package, you should run it with:
java web.proj3.TestClasses.Test CustomerDa o
java web.proj3.TestClasses.Test
you class should also be in the directory:
C:\jwsdp-1.3\webapps\web\p roj3\TestC lasses
and add C:\jwsdp-1.3\webapps to your classpath
C:\jwsdp-1.3\webapps\web\p
and add C:\jwsdp-1.3\webapps to your classpath
ASKER
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?
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?
> so it cud be a class path problem?
Your classpath needs to include the directory C:\jwsdp-1.3\webapps
Your classpath needs to include the directory C:\jwsdp-1.3\webapps
Are the class files in directory:
C:\jwsdp-1.3\webapps\web\p roj3\TestC lasses
C:\jwsdp-1.3\webapps\web\p
ASKER
the current class is
but up one dir is where the files are imported
but up one dir is where the files are imported
> but up one dir is where the files are imported
imported by what?
imported by what?
How many TestCustomerDao do you have on your system gerrymod?
ASKER
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(Http ServletReq uest 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("cust omerName") ;
ArrayList customers = dao.getCustomersByName(cus tomerName) ;
/*String customerName = request.getParameter("cust omerName") ;
ArrayList customers = dao.getCustomersByName(cus tomerName) ;
if (customers.size() == 0)
{
request.setAttribute("erro rMessage", "No customers with this name") ;
page = errorPage ;
}
else
{
request.setAttribute("cust omers", customers) ;
page = "/" + request.getParameter("comm and") + ".jsp" ; // i.e. /CustomersByName.jsp
}*/
if (customers.size() == 0)
{
request.setAttribute("erro rMessage", NO_CUSTOMERS) ;
page = errorPage ;
}
else
{
request.setAttribute("cust omers", customers) ;
page = "/" + request.getParameter("comm and") + ".jsp" ; // i.e. /Customers.jsp
}
}
catch(DaoException e)
{
page = errorPage ;
request.setAttribute("erro rMessage", e.getMessage() ) ;
}
return page ;
}
}
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(Http
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("cust
ArrayList customers = dao.getCustomersByName(cus
/*String customerName = request.getParameter("cust
ArrayList customers = dao.getCustomersByName(cus
if (customers.size() == 0)
{
request.setAttribute("erro
page = errorPage ;
}
else
{
request.setAttribute("cust
page = "/" + request.getParameter("comm
}*/
if (customers.size() == 0)
{
request.setAttribute("erro
page = errorPage ;
}
else
{
request.setAttribute("cust
page = "/" + request.getParameter("comm
}
}
catch(DaoException e)
{
page = errorPage ;
request.setAttribute("erro
}
return page ;
}
}
>>
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.nosuchmethoderro r: main
>>
The class you just posted has no main method. You are trying to execute something as an application that is not an application
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.nosuchmethoderro
>>
The class you just posted has no main method. You are trying to execute something as an application that is not an application
ASKER
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
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
> 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.
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.
>>exception in thread "main" java.lang.nosuchmethoderro r: 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.NoSuchMethodErro r: 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.NoSuchMethodErro
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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
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