*
The moose likes Beginning Java and the fly likes can't compile a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't compile a servlet

Moin Khatri
Greenhorn

Joined: Mar 18, 2007
Posts: 6
Hi,

I cannot compile a servlet BeerSelect.java. when I try to compile this servlet from the command line using this command I get the following errors

javac -d classes src/com/example/web/BeerSelect.java



E:\MyProjects\beerV1>javac -d classes src/com/example/web/BeerSelect.java
src/com/example/web/BeerSelect.java:2: package com.example.model does not exist
import com.example.model.*;
^
src/com/example/web/BeerSelect.java:26: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be = new BeerExpert();
^
src/com/example/web/BeerSelect.java:26: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be = new BeerExpert();
^
3 errors

Here are the source files:

package com.example.web;
import com.example.model.*;


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class BeerSelect extends HttpServlet {


public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Moieen's Beer Selection Advice<br>");

String c = request.getParameter("color");

BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext()){
out.print("<br>try: " + it.next());
}


}

BeerExpert class:

package com.example.model;

import java.util.*;

public class BeerExpert {

public List getBrands ( String color) {

List brands = new ArrayList();

if(color.equals("amber")) {

brands.add("Jack Amber");

brands.add("Red Moose");
}

else {

brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}

return(brands);
}
}

Please can someone help me resolve this?

Thanks in advance,
Moieen
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi,
The compiler is unaware of the location where the package 'com.example.model' and the class 'BeerExpert' present inside it.

You need to add the classpath as a command line argument when invoking javac.

You can do two things.

  • Temporarily: Try giving "javac -classpath <location-to-BeerExpert> -d classes BeerServlet.java". But this setting will be done for this particular invocation only. You need to append the classpath everytime you compile.
  • Permanently: You can add the top folder to the classpath entry so that it will be permanently existing whenever you do compile as the compiler starts searching for the resources from the classpath environment variable. The directory to be added in the classpath is the parent directory of "com" in the "com.example.model" is present



  • HtH.


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Also please have a look at this link Setting Classpath
    Moin Khatri
    Greenhorn

    Joined: Mar 18, 2007
    Posts: 6
    Hi ,

    Thanks for replying back Raghavan.

    I tried adding the path E:\MyProjects\beerV1\src\com to the CLASSPATH env variable but I still get this error


    E:\MyProjects\beerV1>javac -d classes src/com/example/web/BeerSelect.java
    src/com/example/web/BeerSelect.java:2: package com.example.model does not exist
    import com.example.model.*;
    ^
    src/com/example/web/BeerSelect.java:26: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    src/com/example/web/BeerSelect.java:26: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    3 errors

    Please can you let me know what possibly could I be doing wrong and also I tried to compile from cmd line and also did permanently set the classpath but it still doesn't work

    Thanks and Regards,
    Moieen
    Moin Khatri
    Greenhorn

    Joined: Mar 18, 2007
    Posts: 6
    Hi ,

    Thanks for replying back Raghavan.

    I tried adding the path E:\MyProjects\beerV1\src\com to the CLASSPATH env variable but I still get this error


    E:\MyProjects\beerV1>javac -d classes src/com/example/web/BeerSelect.java
    src/com/example/web/BeerSelect.java:2: package com.example.model does not exist
    import com.example.model.*;
    ^
    src/com/example/web/BeerSelect.java:26: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    src/com/example/web/BeerSelect.java:26: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be = new BeerExpert();
    ^
    3 errors

    Please can you let me know what possibly could I be doing wrong and also I tried to compile from cmd line and also did permanently set the classpath but it still doesn't work

    Thanks and Regards,
    Moieen
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Hi Moieen,

    What you did is right but still was not perfect. You have to add *the parent directory* of the starting folder.

    Case 1: If your file(s) does not reside in a package:

    Lets Say, you have an application where in you have two classes A.java and B.java.

    A.java resides in a different folder say "C:\JavaPractice\Folder1\A.java".



    B.java resides in a different folder say "C:\JavaPractice\Folder2\B.java".



    Assume your B.java refers/includes your A.java. As both of them belonging to the same package (default package) {since there is no explicit package mentioned in the source code}, you dont need to import the A class. Directly you can use in the source code.

    But while compiling, the compiler has to be told about where A.class resides. For which you can add the classpath while invoking "javac" in the classpath during runtime. (Case 1 as in my previous reply).



    Note: The entry "C:\JavaPractices" means "C:\JavaPractices\*.class" implicitly!

    Actually -classpath argument carries the location to search for the included class(es) inside the file being compiled (B.java). The included location "C:\JavaPractices" does actually contain the file A.class. So the compilation of B.java would be successful. If you miss the -classpath argument during javac invocation, you will get an error saying "cannot resolve symbol" by pointing out the class "A" .

    Case 2: If your class(es) resides within a package

    Now lets say, A.java resides in a package named "MyPackage" and you declare it as "package mypackage;" as the first line in your A.java.

    For example, your A.java resides in "C:\JavaPractices\mypackage\A.java" and B.java resides in "C:\JavaPractices\B.java".

    Inside B.java when refer the A.java file, you *should* import it like "import mypackage.*;". {or simply "import mypackage.A;" to just refer the A class alone}.

    When including the classfile during compilation, you have to include the parent folder of the file including its package. In this case, where your A.java resides? Its inside the package "mypackage".

    As with previous example, you have to include the "parent directory" of the starting directory/package where the actual class file reside. In our case, the A.class file resides inside the folder "mypackage" which is inside "C:\JavaPractice" as "C:\JavaPractice\mypackage\A.class".

    So you need to include "C:\JavaPractice" into the classpath so that it can start searching for the A.class as with the reference to its package like "mypackage.A" -> mypackage/A.

    I think now you got the point. You need to add the PARENT DIRECTORY of your "com" directory and not including it, so that the compiler will search for the file "com.example.model.BeerExpert" in the folder "com/example/model/".

    In your classpath with whatever you have set now, it will start searching for the file "com/example/model/Example.class" inside the folder "E:\MyProjects\beerV1\src\com" but actually there is no folder named "com" present inside it.

    Note:
    ------
  • If the class you refer exists in a jar file, "javax.servlet.http.HttpServlet" which resides in "servlet.jar", you have to mention the jar file explicitly like




  • If you just give "E:\MyProjects\j2ee\lib" it will search for the referred file "HttpServlet.class" inside the "lib" folder and it will not get it. So you will again get the error!

  • I assumed you have not overloaded the default location of classfiles. If you have put the class files somewhere else, say "E:\MyProjects\classfiles\mypackage\A.class", then you have to give the appropriate location where *class* file resides ie, "E:\MyProjects\classfiles"!


  • Hope you understood it now.
    [ May 29, 2007: Message edited by: Raghavan Muthu ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: can't compile a servlet
     
    Similar Threads
    Problem in making Beer Expert app from Head First Servlet Book
    Head First Servlet : Beer Version 2 Problem
    Problem in accessing model in diffeerent package(Servlet-HFS)
    weird problem
    can't import own model