Cant compile file due to Class problem
I am trying to get my java class to compile.
My first one (the bean) compiles but the second one that calls the bean class doesnt compile:
bean class file that compiles and works:
package beans;
publicclass Customer
{
private String _id;
private String _firstName;
private String _lastName;
private String _address;
public Customer(String id, String firstName, String lastName, String address)
{
_id = id;
_firstName = firstName;
_lastName = lastName;
_address = address;
}
public String getAddress()
{
return _address;
}
publicvoid setAddress(String address)
{
_address = address;
}
public String getFirstName()
{
return _firstName;
}
publicvoid setFirstName(String firstName)
{
_firstName = firstName;
}
public String getLastName()
{
return _lastName;
}
publicvoid setLastName(String lastName)
{
_lastName = lastName;
}
public String getId()
{
return _id;
}
publicvoid set_id(String id)
{
_id = id;
}
}
The file that wont compile:
package beans;
import java.util.ArrayList;
import java.util.List;
publicclass CustomerManager
{
public List getCustomers()
{
return generateCustomers();
}
private List generateCustomers()
{
List rv =new ArrayList();
for (int i = 0; i < 10; i++)
{
rv.add(getCustomer(String.valueOf(i)));
}
return rv;
}
public Customer getCustomer(String id)
{
returnnew Customer(id, id +"First","Last" + id,
"123 Caroline Road Fooville");
}
}
The error message:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\test\WEB-INF\clas
ses\beans>javac CustomerManager.java
CustomerManager.java:26: cannot find symbol
symbol : class Customer
location: class beans.CustomerManager
public Customer getCustomer(String id)
^
CustomerManager.java:28: cannot find symbol
symbol : class Customer
location: class beans.CustomerManager
return new Customer(id, id + "First", "Last" + id,
^
2 errors
Both are located in:
Tomcat 5.5 Home\webapps\test\WEB-INF\classes\beans
It is because it can't find the customer class in the current classpath.
It is assuming the classpath is rooted in the current directory. Seeing as you are in the directory WEB-INF/classes/beans, it is having difficulty.
Try it from the WEB-INF/classes directory instead with: javac beans\CustomerManager.java
ie this:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\test\WEB-INF\clas
ses>javac beans\CustomerManager.java
or alternatively from the beans directory:
javac -classpath .. CustomerManager.java
Would be equivalent.
In both cases the classpath rott has to be the WEB-INF/classes directory so it can find the referenced beans.Customer class.
Cheers,
evnafets
Thanks for replies.
Here is my append to classpath:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\test\WEB-INF\clas
ses>Set CLASSPATH=%CLASSPATH%;C:\Program Files\Apache Software Foundation\Tomcat
5.5\common\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomc
at 5.5\webapps\test\WEB-INF\classes
And here is what I tried but got error message:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\test\WEB-INF\clas
ses>javac beans\CustomerManager.java
Note: beans\CustomerManager.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Please advise.
Its a warning. You can ignore it, and your program will run correctly.
To get the full details, follow the instructions and run it with Xlint:
>javac -Xlint:unchecked beans\CustomerManager.java
Produces this error message:
beans\CustomerManager.java:19: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
rv.add(getCustomer(String.valueOf(i)));
^
1 warning
It is a Java1.5 specific warning because you are not using the new Generics info with the collections.
Here is the version that compiles with no warnings whatsoever.
package beans;
import java.util.ArrayList;
import java.util.List;
public class CustomerManager
{
public List<Customer> getCustomers()
{
return generateCustomers();
}
private List<Customer> generateCustomers()
{
List<Customer> rv = new ArrayList<Customer>();
for (int i = 0; i < 10; i++)
{
rv.add(getCustomer(String.valueOf(i)));
}
return rv;
}
public Customer getCustomer(String id)
{
return new Customer(id, id + "First", "Last" + id,
"123 Caroline Road Fooville");
}
}
Basically the change was everywhere you have List or ArrayList replace it with List<Customer> - meaning you have a List of customer objects, rather than just a List of "anything"
If you are compiling with jdk1.5, then anywhere you use a Collection without specifying the types in this manner, it will create a warning.
You can ignore the warning if you so wish, and the program should run fine.
Cheers,
evnafets