"PrintWriter" is not recognized by eclipse

10,022

Solution 1

Er, yes, you need to import java.io.PrintWriter. Or java.io.*.

Solution 2

You need to import java.io.PrintWriter.

Eclipse provides a useful shortcut to Organize Imports, Ctrl + Shift + O. By default this imports each class explicitly.

Solution 3

All you need is the following

   import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Share:
10,022
Yogi Sachdev
Author by

Yogi Sachdev

Updated on June 17, 2022

Comments

  • Yogi Sachdev
    Yogi Sachdev almost 2 years

    I have Eclipse(Kepler) IDE and when I write the below code.

    PrintWriter pw=response.getWriter();
    

    It displays an error message saying that PrintWriter cannot be resolved to type. The import statements that I have used are as follows,

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.*;
    

    Do I have to import some additional statements to get my PrintWriter to work? If yes then which are they?

    Thanks in advance :)

    • andrew cooke
      andrew cooke over 10 years
      why do you have javax.servlet.http.* and specific classes in javax.servlet.http?
    • Yogi Sachdev
      Yogi Sachdev over 10 years
      @andrewcooke actually they are automatically imported by Eclipse when I define a Servlet
    • andrew cooke
      andrew cooke over 10 years
      that doesn't make it right.
    • Yogi Sachdev
      Yogi Sachdev over 10 years
      @andrewcooke yeah I know that dude I was just saying the reason why they were there.
  • Stephen C
    Stephen C over 10 years
    Bad advice! Over use of wildcard imports makes your code fragile when you attempt to recompile against new releases of Java / new class libraries.