Skip to main content

Java error: Only a type can be imported. XYZ resolves to a package


I get the error: "Only a type can be imported. XYZ resolves to a package."



Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The java.util.* import works fine.




<%@ page import="java.util.*"%>
<%@ page import="org.ivec.eresearch.knowledgeportal.model.Category"%>
<%@ page import="org.ivec.eresearch.knowledgeportal.dao.CategoryDao"%>

<%
CategoryDao catDao = new CategoryDao();
ArrayList<Category> catList = catDao.selectCategory();

//
%>



Edit: the actual error is below:




org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
Only a type can be imported. org.ivec.eresearch.knowledgeportal.model.Category resolves to a package


Source: Tips4all

Comments

  1. Well, you are not really providing enough details on your webapp but my guess is that you have a JSP with something like that:

    <%@ page import="java.util.*,x.y.Z"%>


    And x.y.Z can't be found on the classpath (i.e. is not present under WEB-INF/classes nor in a JAR of WEB-INF/lib).

    Double check that the WAR you deploy on Tomcat has the following structure:

    my-webapp
    |-- META-INF
    | `-- MANIFEST.MF
    |-- WEB-INF
    | |-- classes
    | | |-- x
    | | | `-- y
    | | | `-- Z.class
    | | `-- another
    | | `-- packagename
    | | `-- AnotherClass.class
    | |-- lib
    | | |-- ajar.jar
    | | |-- bjar.jar
    | | `-- zjar.jar
    | `-- web.xml
    |-- a.jsp
    |-- b.jsp
    `-- index.jsp


    Or that the JAR that bundles x.y.Z.class is present under WEB-INF/lib.

    ReplyDelete
  2. OK I just solved it. In the last import I added a ";" by copying other code examples. I guess it's the standard line ending that is required.

    So

    <%@ page import="java.util.*" %>
    <%@ page import="org.ivec.eresearch.knowledgeportal.dao.CategoryDao" %>
    <%@ page import="org.ivec.eresearch.knowledgeportal.model.Category" %>


    became

    <%@ page import="java.util.*" %>
    <%@ page import="org.ivec.eresearch.knowledgeportal.dao.CategoryDao" %>
    <%@ page import="org.ivec.eresearch.knowledgeportal.model.Category;" %>

    ReplyDelete
  3. Without further details, it sounds like an error in the import declaration of a class. Check, if all import declarations either import all classes from a package or a single class:

    import all.classes.from.package.*;
    import only.one.type.named.MyClass;


    Edit

    OK, after the edit, looks like it's a jsp problem.

    Edit 2

    Here is another forum entry, the problem seems to have similarities and the victim solved it by reinstalling eclipse. I'd try that one first - installing a second instance of eclipse with only the most necessary plugins, a new workspace, the project imported into that clean workspace, and hope for the best...

    ReplyDelete
  4. You have to import something FROM the package, like a class, enum, or interfacee, like this:

    import some.package.SomeClass;


    or, import everything from the package (not recommended)

    import some.package.*;


    edit: maybe I didn't read close enough. Where is the package you're trying to import from located on the filesystem? Is it under WEB-INF/lib?

    ReplyDelete
  5. Take a look at this link here. They seem to have similar problem in Eclipse. Not sure if you will find the solution there but the last message has some suggestions that you can try out.

    ReplyDelete
  6. Are there more details? (Is this in a JSP as in the linked webpage?)

    If so, you can always just use the fully qualified class name.
    rather than:

    import foo.bar.*;
    Baz myBaz;


    you can use

    foo.bar.Baz myBaz;

    ReplyDelete
  7. i know it's kinda too late to reply to this post but since i don't see any clear answer i'd do it anyway...

    you might wanna check out the MANIFEST.MF in META-INF on your eclipse.

    then you might need to add the path of your class files like..

    Class-Path: WEB-INF/classes

    hope it helps. :)

    ReplyDelete
  8. I experienced this weird error too, after changing letter case in the name of a class. The file was not copied to a tomcat server as expected, I had to delete it manually and redeploy. Maybe because I use case insensitive operating system?

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.