File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Unable to import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to import" Watch "Unable to import" New topic
Author

Unable to import

Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
Hi all

I am unable to import the javax.servlet.http.*;
please help
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

What are you trying to do? Unable to compile a servlet? If so, you need servlet-api.jar in your classpath. Tell us what you are trying to accomplish and how you are doing it. Then we can help easily.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
i am doing the beerV1 project from the head first.
i included the class path but it saying the package javax.servlet.http.*; does not exist
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

When do you get this error? Show us the stack trace. Have you setup the classpath ? If so how? Try "echo %CLASSPATH%" (on Windows) from command prompt and post the output.
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
Vijitha Kumara wrote:When do you get this error? Show us the stack trace. Have you setup the classpath ? If so how? Try "echo %CLASSPATH%" (on Windows) from command prompt and post the output.


the class path is ok.it is compile time error in which it is unable to find the package
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
this is the class path
.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

it is compile time error in which it is unable to find the package


That's because the required jar files (servlet-api.jar in this case) not found in the classpath.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Anky Gupta wrote:this is the class path
.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip


There is the problem. You have to add an entry to refer the servlet API implementation in the classpath which can be found among your container's library files (probably inside "YOURCONTAINERHOME/lib"). BTW, which container you are using?
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101

D:\Prog\MyProject\beerV1>javac -classpath \C:\ProgramFiles\Apache\Tomcat\common\
lib\servlet-api.jar;classes:. -d classes src\com\example\web\BeerSelect.java
src\com\example\web\BeerSelect.java:3: package javax.servlet does not exist
import javax.servlet.*;
^
src\com\example\web\BeerSelect.java:4: package javax.servlet.http does not exist

import javax.servlet.http.*;
^
src\com\example\web\BeerSelect.java:7: cannot find symbol
symbol: class HttpServlet
public class BeerSelect extends HttpServlet {
^
src\com\example\web\BeerSelect.java:9: cannot find symbol
symbol : class HttpServletRequest
location: class com.example.web.BeerSelect
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOExce
ption,ServletException
^
src\com\example\web\BeerSelect.java:9: cannot find symbol
symbol : class HttpServletResponse
location: class com.example.web.BeerSelect
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOExce
ption,ServletException
^
src\com\example\web\BeerSelect.java:9: cannot find symbol
symbol : class ServletException
location: class com.example.web.BeerSelect
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOExce
ption,ServletException

^
6 errors



This the command prompt
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Anky, Check my previous reply. You will be fine after adding the servlet API jar file to your classpath
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip;C:\Program Files\Apache\Tomcat\common\lib


IS this class path okay?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Anky Gupta wrote:.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip;C:\Program Files\Apache\Tomcat\common\lib

IS this class path okay?


Almost. Just make it,

C:\Program Files\Apache\Tomcat\common\lib\servlet-api.jar
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip;C:\Program Files\Apache\Tomcat\common\lib\servlet-api.jar

I have set this as class path in the Environment Variable but still no avail
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Just type,

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

If you got any error post it here.

Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
Yippe finally compiled

Much appreciated Thanks.
Thank you for having patience with me.
you are such a nice person

Cheers
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

No problem
Ilakya Mukunth
Ranch Hand

Joined: Mar 13, 2012
Posts: 57
I have an issue in the same example. I created two files (shown below). But the only changes I made in the directory structure is that, I removed the com.example. Please look at my directory structure below. I am unable to import the model package from BeerSelect.java

BeerExpert.java
===========
package model;
import java.util.*;
public class BeerExpert {
public List getBrand(String color) {
List brands = new ArrayList();
if (null != color && color.equals("amber"))
{
brands.add("Jack Amber");
brands.add("Red Moose");
}
else
{
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
}
return brands;
}
}
Compile:
=======
C:\ProjectWork\Beer>javac -classpath "C:\Program Files\Tomcat\lib\servlet-api.jar" -d classes src/model/BeerExpert.java
Note: src\model\BeerExpert.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

BeerSelect.java
==========

package web;
import model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String color = request.getParameter("color");
out.println("Beer selection <br> color is: " +color);
}
}

Compile:
======
C:\ProjectWork\Toys>javac -classpath "C:\Program Files\Tomcat\lib\servlet-api.jar" -d classes src/web/BeerSelect.java
src\web\BeerSelect.java:10: error: package model does not exist
import model.*;
^
1 error


Please find my directory structure in the attachment.
Kindly let me know your views ASAP.
Thanks in advance,
SPRK




[Thumbnail for unable_to_import.jpg]

sreenu valireddi
Greenhorn

Joined: Apr 23, 2014
Posts: 18



I think for both of them working directory should be same. But you are using diffeerent directories



C:\ProjectWork\Beer>
C:\ProjectWork\Toys>




J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1003
    
  13

Setting the classpath in the environment is Very Bad Idea. It leads to all sorts of hard to analyze errors. Just use the "-cp" option on the javac command line when you compile.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to import