• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to import

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am unable to import the javax.servlet.http.*;
please help
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the class path
.;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anky, Check my previous reply. You will be fine after adding the servlet API jar file to your classpath
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.;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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.;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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just type,

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

If you got any error post it here.

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yippe finally compiled

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

Cheers
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem
 
Ilakya Mukunth
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

unable_to_import.jpg
[Thumbnail for unable_to_import.jpg]
unable to import
 
sreenu valireddi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 1745
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic