facebook

"The import java.io.* is never used" warning in JS

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #203489 Reply

    iimuhin
    Member

    Hi all,

    i get “The import java.io.* is never used” warning in JSPs, in which it’s really used. And if I delete this line from the imports und run the JSP under Tomcat, i get the error (for example) “Class File is not found in imports”.

    Thanks,
    Igor.

    #203492

    Riyad Kalla
    Member

    Igor,
    Unless you are using every single class in the java.io package, I believe this error is accurate. Can you full qualify each import separately?

    #203497

    iimuhin
    Member

    Hi support-rkalla,

    I suppose that MyEclipse’s JSPCompiler adds the line “import java.io.*” during generation of java files from jsp files. And if a JSP already contains this import he replys with “Import java.io.* is never used”.

    Tomcat in his turn doesn’t add his own “import java.io.*” during compilation. And it will be followed by “Class File not found” if I delete JSP’s import java.io.*.

    I think, MyEclipse’s JSPCompiler shouldn’t add his own “import java.io.*”.

    Thanks
    Igor.

    #203505

    Scott Anderson
    Participant

    Igor,

    I don’t follow this. The JSP compiler doesn’t add any imports to your files. It simply compiles them. Can you post a small example JSP that exhibits the behavior you’re having a problem with and explain what it’s doing and what you expect to be different?

    #203562

    iimuhin
    Member

    Hi All,

    an example for this:

    <%@ page import="java.io.*" %>
    <%
        File f = new File("c:\\Temp");
        j = 5;
    %>

    I made an error (j is not defined) consciously to make MyEclipse to show compilation warinings (it shows warnings only if there’re errors).
    In this case you will see two messages: 1) error: j can not be resolved and 2) warning: The import java.io.* is never used.

    Through the import is used. Why do it shows this message?

    Thanks,
    Igor.

    #203568

    Riyad Kalla
    Member

    Igor,
    Thank you for the test case, I can see exactly the behavior your are talking about, this is strange.

    Scott:
    1) Create a new JSP page, compile it (save).
    2) Note that there are no errors or warnings
    3) Add this snippet of code to it and save:

    
    <%
        File f = new File("c:\\Temp");
        j = 5;
    %>
    

    2) Note that you now have 3 errors. 2 unused imports and 1 error on the line with ‘j = 5’. I believe this is Problem #1.
    3) Now remove the ‘j = 5’ line, and save it. All the problems go away, even though you have no import for java.io.File. I believe this is Problem #2.
    4) Now add the line ‘FileInputStream fs = new FileInputStream(“hello”);’ directly underneath the “File f = …’ code, save it. Note, still no errors.

    So now I understand what Igor was saying, it does seem like java.io import is being ‘added’ or atleast ignored on the JSP page when it is compiled. Even when you remove all the default imports, and have no imports; it still compiles. The other problem he saw that I can reproduce is that unused imports don’t see to be reported unless there is an error with the file.

    #203621

    support-michael
    Keymaster

    I will have to search the spec but I believe that the packages:

    java.lang.*
    java.io.*

    are automatically visible to every JSP. Otherwise how would out.println work. For some reason I thought java.util.* was also visible but am less certain. We have already pulled java.lang.* and java.io.* imports from the template.

    #203622

    Riyad Kalla
    Member

    Michael,
    I looked into this and created the following JSP page:

    
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'MyJsp.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        This is my JSP page. <br>
      </body>
    </html>
    

    note this is the default MyJsp.jsp template and I removed the imports. The code that was generated in a Tomcat 5.0.18 container was the folllowing:

    
    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    
    public final class MyJsp_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
      private static java.util.Vector _jspx_dependants;
    
      public java.util.List getDependants() {
        return _jspx_dependants;
      }
    
      public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {
    
        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        JspWriter _jspx_out = null;
    
    
        try {
          _jspxFactory = JspFactory.getDefaultFactory();
          response.setContentType("text/html");
          pageContext = _jspxFactory.getPageContext(this, request, response,
                      null, true, 8192, true);
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;
    
    
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    
          out.write("\n\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html>\n  <head>\n    <base href=\"");
          out.print(basePath);
          out.write("\">\n    \n    <title>My JSP 'MyJsp.jsp' starting page</title>\n    \n    <meta http-equiv=\"pragma\" content=\"no-cache\">\n    <meta http-equiv=\"cache-control\" content=\"no-cache\">\n    <meta http-equiv=\"expires\" content=\"0\">\n    \n    <meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\n    <meta http-equiv=\"description\" content=\"This is my page\">\n    \n    <!--\n    <link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\n    -->\n  </head>\n  \n  <body>\n    This is my JSP page. <br>\n  </body>\n</html>\n");
        } catch (Throwable t) {
          if (!(t instanceof SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(t);
          }
        } finally {
          if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
      }
    }
    

    which doesn’t seem to have any default java.* imports in it. And it seems the reason out works is because its an implied object from the line:

    
    out = pageContext.getOut();
    

    ideas?

    #203623

    support-michael
    Keymaster

    ideas?

    Ahhhh!
    <foot disposition=”in mouth”/>

    <foot disposition=”normal”/>
    No! 🙂

    #203624

    Riyad Kalla
    Member

    hahah

    Are you guys able to uncover anything sinister going on behind the BJE-scenes?

Viewing 10 posts - 1 through 10 (of 10 total)
Reply To: "The import java.io.* is never used" warning in JS

You must be logged in to post in the forum log in