3

When I write out.println(), eclipse complains that out cannot be resolved.

I have imported java.io.* and other servlet packages.

Sirko
  • 72,589
  • 19
  • 149
  • 183
London guy
  • 27,522
  • 44
  • 121
  • 179

7 Answers7

9

Just a shot in the dark, I think this is the out you are looking for:

public class OutServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("foo");
    }
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
5

Use:

import static java.lang.System.out;
Sirko
  • 72,589
  • 19
  • 149
  • 183
NominSim
  • 8,447
  • 3
  • 28
  • 38
4

You should write System.out.println(); inside a function.

If you write it directly into the class then it might show the error that you are having right now.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
Pankaj Joshi
  • 57
  • 1
  • 2
2

Import it using a static import:

    import static java.lang.System.out;

However, I'd recommend that you don't do this.

  • Using the full name makes references to System.out stand out, and makes them easier to "grep" for ... if you need to nuke traceprints.

  • If you need to write a lot of stuff to the console, you should make out a variable or a method parameter. This will help to make your code more reusable; e.g. so that it can write to somewhere other than System.out.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

I had this problem. I found out I didn't have main method( public static void main(string[]args) and that is why it was giving me this error. Hope this helps.

Goblinkiller
  • 99
  • 1
  • 7
0

If your are looking to put some debug outputs, you can do this:

System.out.println("foo");

If your are looking for adding the output to HTML not printing on debug console you can do as follow: First you should add the 'servlet-api.jar' to your project. Then simply you can use this if you want to add your output to HTML:

response.getWriter().println("foo");

I hope it helps.

Saadat
  • 461
  • 6
  • 9
-2

Can you please check whether you are having any class name as "System" ?
This could be one reason.
Like in the code below if I use class name as System it would end up with the same Error like yours.

Cannot use Identifier 'System' as Class name;

public class System {

public static void main(String[] args) {

    int A[] = { 3, 9, 7, 8, 12, 6, 15, 5, 4, 10 };

    for (int x : A) {
        System.out.print(x + ", ");

    }
    System.out.println("\n*******************");
    int t = A[A.length - 1];

    System.out.println(t);

}

}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • 1
    The answer is far away from topic. Even so you should try to propose something which will solve the issue, not another way of reproduce. Note also, is a very old question and most likely the OP issues is long solved (even if not, does OP still have the original code ? and environment in order to reproduce ? is OP still online?). Regarding off-topic, kindly look at tags and do some research regarding servlets. Main reason for down-voting is off-topic. – Traian GEICU Jul 26 '22 at 01:39