"Import cannot be resolved" with JSP

23,685

Solution 1

you should give fully qualified name for your class. (packagename.classname) like:

    <%@ page import="pkgname.Class1"%>

Solution 2

Page directives are normally placed at the top of a JSP. Also I assume Class1 is in the default package since it does not possess a fully qualified name. If Class1 is in a package you need to prefix the name in the import with the package name.

<%@ page import="java.util.*" %>
<%@ page import="Class1" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
</head>
<body>
    <p>
<%
Class1 tc=new Class1("test");
out.print(tc.str);
  %>
    </p>
</body>
Share:
23,685
user1884132
Author by

user1884132

Updated on July 05, 2022

Comments

  • user1884132
    user1884132 almost 2 years

    I am trying to call a Java class from a JSP page. I have created the project using JDeveloper.

    I am getting an error that says "The import cannot be resolved". I have added the Class file in WEB-INF, root folder, and tried compiling, but it still shows the same error.

    Below is the code:

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
        </head>
        <body>
            <p>  
                <%@ page import="java.util.*"%>
                <%@ page import="Class1"%>
                <% 
                    Class1 tc=new Class1("test");
                    out.print(tc.str);
                %>
            </p>
        </body>
    </html>
    
  • PermGenError
    PermGenError over 11 years
    i dont think it'd really matter declaring them in the body tag
  • Kevin Bowersox
    Kevin Bowersox over 11 years
    @GanGnaMStYleOverFlowErroR this was more a style or best practice suggestion.
  • PermGenError
    PermGenError over 11 years
    @km yes, its a good practice, but wouldn't solve the compiler error , would it ?
  • Kevin Bowersox
    Kevin Bowersox over 11 years
    @GanGnaMStYleOverFlowErroR agreed. I'm also interested in if the class is packaged. I used to work with Jdeveloper, I feel for anyone using that "tool".
  • PermGenError
    PermGenError over 11 years
    @km as we can see from his JSp, that could be the only possible case.
  • user1884132
    user1884132 over 11 years
    the class is not packaged. Is packaging necessary?
  • user1884132
    user1884132 over 11 years
    Hi all, thanks for quick replies.... The class "Class1" is not packaged. If I type a different class name (class that does not exists), then JDeveloper shows error "class not found" with yellow underline. If I type correct class name, editor error is resolved, however, I get that error when I compile. I have tried adding class path of that directory, but error kept showing up. Thanks.
  • PermGenError
    PermGenError over 11 years
    @user1884132 yes, you have to package your class's. its a bad practice if dont package em. check this link for more details coderanch.com/how-to/java/PackageYourBeans
  • Richard Bradley Smith
    Richard Bradley Smith about 5 years
    I had a similar error which brought me here. I had <%@ page import="main.java.model.*" %> which resulted in CompanyDAO not being resolved when the JSP ran. I repeated the line and replaced the * with CompanyDAO like this: <%@ page import="main.java.model.CompanyDAO" %>. Much to my surprise that resolved the issue. And yes, take the time to put all your code in packages. I don't care much for "CodeRanch". I figured out packages in Eclipse by converting the mess I had made into what Eclipse wanted for a Web app.