2

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Amy
  • 31
  • 2
  • Making few modifications to the servlet code, i wrote a jsp file called result.jsp and placed it in C:\Program Files\Tomcat 6.0\webapps\amy1\.The new servlet code which calls jsp file is some thing like dis.. try { RequestDispatcher rd=request.getRequestDispatcher("result.jsp"); rd.forward(request,response); } – Amy Oct 25 '11 at 06:34
  • On my web page when i submit my query, it gives Error 404 : JSP file \amy1\result.jsp cant be found. Can some body again provide help.. ? No clue why d jsp file s not getting found, inspite of placing it in proper directory structure.. – Amy Oct 25 '11 at 06:35
  • pls can anyone provide help regarding this.. ? – Amy Nov 23 '11 at 10:37

4 Answers4

2

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Agreed. Actually it's probably somewhat manageable by hand since classpath wildcards are now supported: http://stackoverflow.com/questions/15496/hidden-features-of-java/1859579#1859579 – oksayt Oct 24 '11 at 08:23
  • Thanks for d help. when i included the path in the classpath. It worked. – Amy Oct 25 '11 at 06:21
1

Several comments

  1. Case of class name is wrong: Mymodel.java vs. MyModel.class.
  2. This line package com.example.web. is wrong: it cannot be terminated by dot and semicolon is missing.

Fix this and try again.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

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"
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614