hi folks,
i am a beginner in JSP, and i am studying headfirst servlet & JSP,but at the time of running a code from there,i have an problem

there is directory structure src/com/example/web/BeerSelect.java

and the code is

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
    {
        String c=request.getParameter("color");
        BeerExpert be=new BeerExpert();
        List result=be.getBrands(c);
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("Beer Selection Advice<br/>");
        Iterator it=result.iterator();
        while(it.hasNext())
        {
            out.print("<br/>try: "+it.next());   
        }
    }
}

now at the time of running this code form cmd i am getting this error

E:\JAVA\JSP\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d clas
ses src/com/example/web/BeerSelect.java
src\com\example\web\BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
src\com\example\web\BeerSelect.java:14: cannot find symbol
symbol  : class BeerExpert
location: class com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                ^
src\com\example\web\BeerSelect.java:14: cannot find symbol
symbol  : class BeerExpert
location: class com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                                  ^
3 errors

the path which is declred in import statement is like

src/com/example/model/BeerExpert.java

and the code for BeerExpert.java is here

package com.example.model;
import java.util.*;

public class BeerExpert
{
    public List getBrands(String color)
    {
        List<String> brands=new ArrayList<String>();
        if(color.equals("amber"))
        {
            brands.add("Jack Amber");
            brands.add("Red Moose");
        }
        else
        {
            brands.add("Jail Pale Ale");
            brands.add("Gout Stout");
        }
        return(brands);
    }

}

i have already set the classpath and path variable,it is verymuch helpful for me ,if anyone tell me where the problem is???

Recommended Answers

All 11 Replies

in BeerExpert.java you have imported package com.example.model, but your BeerSelect.java is in different package.

Just i don't get these thing.will you please explain?

@jalpesh 007

first of all thnx for replying,

basically the com.example.model package is for model and the com.example.web package where BeerSelect.java resides,is using the BeerExpert class from the model,thats why i am importing

import com.example.model.*;

do you have any solution?????

i think you just have to import everything.
just try with

import com.example.*;

and run your code.
I hope its might be working.

@jalpesh_007

it's not working.....I have already tried every method written above......but it still gives same error.....

Can you please provide the "full path" of the src folder.I think src folder is not inside any server like TOMCAT , Hence you are getting this error.

for example: d:\temp\src ( it may be like this and i am only guessing).

Normally if your code is deployed on TOMCAT server , then the parent-folder having the complete code will be inside the webpps folder of TOMCAT server.
If the application name is Examples1 .
Then inside TOMCAT-webapps-folder there will be another folder by name a Examples1.

The java source code will be in src-folder which is again inside Examples1- folder.

But when you write code for import you will not write like below

import src.com.org.test;

but you will write only as below

import com.org.test;

This is because TOMCAT server knows that "src" is the parent folder where java code is present hence src folder is already present in the path/ classpath of tomcat server.

So if your current code is not on server like TOMCAT .
Then you need to import the complete path starting from src
that is

import src.com.example.model.*;

@subramanya.vl,first of all thnx for the response

actually i am following mvc,

and i have a deployment environment and also a development environment

my development env structure is like----

E:\
|---------JAVA
            |------JSP
                    |------beerV1

and inside the beerV1 i have 5 directories

1.src
2.classes
3.etc
4.lib
5.web

now the src directory is like

src
|---------com
            |-------examlpe
                        |--------model & web diectory

now the model directory contains

BeerExpert.java

and the web directory contains

BeerSelect.java

the code BeerExpert.java is as follows

package com.example.model;
    import java.util.*;
    public class BeerExpert
    {
    public List getBrands(String color)
    {
    List<String> brands=new ArrayList<String>();
    if(color.equals("amber"))
    {
    brands.add("Jack Amber");
    brands.add("Red Moose");
    }
    else
    {
    brands.add("Jail Pale Ale");
    brands.add("Gout Stout");
    }
    return(brands);
    }
    }

and the BeerSelect.java code is follows

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
    {
    String c=request.getParameter("color");
    BeerExpert be=new BeerExpert();
    List result=be.getBrands(c);
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    out.println("Beer Selection Advice<br/>");
    Iterator it=result.iterator();
    while(it.hasNext())
    {
    out.print("<br/>try: "+it.next());
    }
    }
    }

so now iam trying to compile the BeerSelect.java and put the class file in the directory named classes under beerV1 an i am also previously put the BeerExpert.class inside the classes directory and the classes directory has the same directory structure like src

classes
|---------com
            |---------example
                            |---------model & web directory

now the BeerExpert.class is inside the model directory and when i am trying to put the BeerSelect.class file inside the web directory I have some error

first of all the command which i used to put the BeerSelect.class file inside the web directory is like

E:\JAVA\JSP\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

and i get the following errors:

E:\JAVA\JSP\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d clas
ses src/com/example/web/BeerSelect.java
src\com\example\web\BeerSelect.java:3: package src.com.example.model does not ex
ist
import src.com.example.model.*;
^
src\com\example\web\BeerSelect.java:14: cannot find symbol
symbol  : class BeerExpert
location: class src.com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                ^
src\com\example\web\BeerSelect.java:14: cannot find symbol
symbol  : class BeerExpert
location: class src.com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                                  ^
3 errors

can you please help...

I am able to reproduce this issue on my system and fix the issue
 Here is what i did
I Have put the source code uder the folder D:\Java\JSP\beerV1>

Now under this there is no src folder(not required)  and  this directory structure ---> "D:\Java\JSP\beerV1>"  as src folder.

so under this(D:\Java\JSP\beerV1>) I have below folder structure



1. D:\Java\JSP\beerV1>_____________________              
2. |---------com
3.             |---------example
4.                             |---------model 
5.                             |---------web




    so the complete path for the   "BeerSelect.java"   is -------> D:\Java\JSP\beerV1\com\example\web\BeerSelect.java


    And so the complete path for the   "BeerExpert.java"   is ------->
    D:\Java\JSP\beerV1\com\example\model\BeerExpert.java


    Full -path for my  servlet-api.jar is ---->
    D:\TOMCAT6_PRECONFIGURED\apache-tomcat-6.0.28\lib

    now i have moved tothe directory "D:\Java\JSP\beerV1\com\example\web\"  to compile my BeerSelect.java

    so 




    d:\
    d:\cd D:\Java\JSP\beerV1\com\example\web\



when I put the below command to complie ---->BeerSelect.java
I get this error   

    D:\Java\JSP\beerV1\com\example\web>"c:\Program Files\Java\jdk1.6.0_03\bin\javac"  -classpath D:\TOMCAT6_PRECONFIGURED\ap
    ache-tomcat-6.0.28\lib\servlet-api.jar; BeerSelect.java
    BeerSelect.java:12: package com.example.model does not exist
    import com.example.model.BeerExpert;
                            ^
    BeerSelect.java:19: cannot find symbol
    symbol  : class BeerExpert
    location: class com.example.web.BeerSelect
            BeerExpert be=new BeerExpert();
            ^
    BeerSelect.java:19: cannot find symbol
    symbol  : class BeerExpert
    location: class com.example.web.BeerSelect
            BeerExpert be=new BeerExpert();    

                          ^
3 errors



    But i add the directory "D:\Java\JSP\beerV1\" to class path in the command itself

    SO my modified command is ------------------------------>



    D:\Java\JSP\beerV1\com\example\web>"c:\Program Files\Java\jdk1.6.0_03\bin\javac"  -classpath D:\TOMCAT6_PRECONFIGURED\ap
    ache-tomcat-6.0.28\lib\servlet-api.jar;D:\Java\JSP\beerV1\  BeerSelect.java

    D:\Java\JSP\beerV1\com\example\web>





See above , the code gets executed without any errors. I verified the contents of folder 
D:\Java\JSP\beerV1\com\example\web>




    D:\Java\JSP\beerV1\com\example\web>dir
     Volume in drive D is Data
     Volume Serial Number is 8AF4-3898

     Directory of D:\Java\JSP\beerV1\com\example\web

    12/03/2012  02:16 PM    <DIR>          .
    12/03/2012  02:16 PM    <DIR>          ..
    12/03/2012  02:28 PM             1,527 BeerSelect.class
    12/03/2012  02:04 PM               950 BeerSelect.java
                   2 File(s)          2,477 bytes
                   2 Dir(s)  127,987,572,736 bytes free



As you can see the java file is now compiled.
So the logic is please provide the classpath in the command itself

 D:\Java\JSP\beerV1\com\example\web>
 "c:\Program Files\Java\jdk1.6.0_03\bin\javac"  -classpath D:\TOMCAT6_PRECONFIGURED\ap
    ache-tomcat-6.0.28\lib\servlet-api.jar;D:\Java\JSP\beerV1\  BeerSelect.java

 you can observe that -classpath is followed by    "d:\TOMCAT6_PRECONFIGURED\ap
    ache-tomcat-6.0.28\lib\servlet-api.jar;D:\Java\JSP\beerV1\"

  Please observe the semi-colon beween two entries in here.

  first entry--> is d:\TOMCAT6_PRECONFIGURED\apache-tomcat-6.0.28\lib\servlet-api.jar
  second entry is--> D:\Java\JSP\beerV1\

@subramanya.vl

basically i am follwing a book name heasfirst servelt and jsp,and it is follwing the mvc

thats why i have put my code in src folder

and i have to complie the code and at the time of compiling the code,the code autometically converyted to .class file and send to the classes directory under beerV1

and the command line is like

E:\java\jsp\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

and my directory structure of claases and src which is under beerV1 is same

src directory

src
|------com
        |------example
                    |---------web(BeerSelect.java)
                    |---------model(BeerExpert.java)

classes directory

classes
|------com
        |------example
                    |---------web(BeerSelect.class)
                    |---------model(BeerExpert.class)

i already put BeerExpert.class file inside the model directory under classes by executing the follwing command

E:\java\jsp\beerV1>javac -d classes src/com/example/model/BeerExpert.java

after that i tried to put the BeerSelect.class file inside the web directory under classes by executing the follwing command

 E:\java\jsp\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

and then the follwing error occured.....

plz help

Please try this ( hope this will be last one)
As per my understanding 
(Under current  scenario)

Full path of BeerSelect.java is ------------------------------>
"E:\java\jsp\beerV1\src\com\example\web\BeerSelect.java"

Full path of BeerExpert.java is------------------------------> "E:\java\jsp\beerV1\src\com\example\model\BeerExpert.java"

To compile BeerSelect.java you should go the directory---> "E:\java\jsp\beerV1\src\com\example\web\"

then type this properly(you can copy paste)

E:\java\jsp\beerV1\src\com\example\web>javac -classpath D:/tomcat/common/lib/servlet-api.jar;E:\java\jsp\beerV1\src  -d classes BeerSelect.java  
======================================================================================

 ANOTHER _ METHOD

You are inside ---> "E:\java\jsp\beerV1\" directory        

 E:\java\jsp\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar;E:\java\jsp\beerV1\src -d classes src/com/example/web/BeerSelect.java         

 Hope now you are able to compile the file --->BeerSelect.java      
commented: thnx a ton man,what i say, iam stuck with this problem for two week but can't find any solution,you are the giving the exact solution,its runnning absolutely fine,again thnx a lot.... +0

@subramanya.vl

thnx a ton man,what i say, iam stuck with this problem for two week but can't find any solution,you are the giving the exact solution,its runnning absolutely fine,again thnx a lot....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.