Reputation: 31
I am designing a web application in Java using MVC architecture. I am using a Tomcat server. My directory structure is as follows:
Myprojects
|
amy1-------------------------
| | | | |
src lib etc web classes
| |
com com
| |
example---------- example
| | | |
web model web model
| | |
MyServlet Mymodel.java MyModel.class
.java
I have used package com.example.model
statement in MyModel.java
.
The code of MyServlet
is something like this:
package com.example.web.
import com.example.model.*;
// ...
String c = request.getParameter();
MyModel m = Mymodel(c);
I am in this directory when I use javac
:
C:\MyProjects\amy1
I execute following javac command:
javac -classpath "C:\Program Files\Tomcat 6.0\lib\servlet-api.jar" -d classes "src\com\example\MyServlet.java"
I get compiler errors saying:
1. import of package com.example.model.* failed.
2. symbol not found error on line where I create Mymodel object.
Can anyone tell me how to solve this bug? Why is the package not getting imported?
Upvotes: 2
Views: 331
Reputation: 308279
Java is case sensitive. So unless you have both a MyModel
class and a Mymodel
class (where the later extends the former), the following code is not correct:
MyModel m = Mymodel(c);
You also mention Mymodel.java
in your directory tree and a MyModell
class in your descriptions. You must always use the same capitalization for your classes.
Upvotes: 0
Reputation: 94653
You have to include a classpath
of com.example.model classes.
>javac -cp .;"C:\MyProjects\amy1\classes";"C:\Program Files\Tomcat 6.0\lib\servlet-api.jar" -d classes "src\com\example\MyServlet.java"
Upvotes: 0
Reputation: 115418
Several comments
package com.example.web.
is wrong: it cannot be terminated by dot and semicolon is missing.Fix this and try again.
Upvotes: 1
Reputation: 692281
You also need to put the classes directory in the classpath, else javac can't find your MyModel
class.
It's really refreshing to see someone using the command line tools to build his classes before using an IDE. That's the best way to learn how javac, java and the classpath work. But when the number of classes grow, it will quickly become harder. I suggest you use Ant to build your classes once you're accustomed to the command line tools.
Upvotes: 2