JSP error: Only a type can be imported. x.y.Z resolves to a package
Asked Answered
U

27

85

I get the error:

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.eresearch.knowledgeportal.model.Category 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.eresearch.knowledgeportal.model.Category"%>
 <%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao"%>

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

 //
 %>
Ulloa answered 7/12, 2009 at 7:46 Comment(3)
It will help if you can post the code and provide more details of your environment.Sultana
Well most of the code is just HTML. Since it falls over at this point I have not progressed much. I am using Tomcat 6 but I am running thing through Eclipse debug option. Let me know if theres anyhing else I can provide.Ulloa
Scriptlets are a bad practice: https://mcmap.net/q/246339/-jsp-for-business-layer-closed/… Use a servlet.Selenite
U
41

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.eresearch.knowledgeportal.dao.CategoryDao" %>
<%@ page import="org.eresearch.knowledgeportal.model.Category" %>

became

 <%@ page import="java.util.*" %>
 <%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
 <%@ page import="org.eresearch.knowledgeportal.model.Category;" %>
Ulloa answered 7/12, 2009 at 8:35 Comment(4)
AFAIK adding the semi-colon gives an error and has the same effect as removing the line.Subtenant
This fixed an issue for me. However after I deleted the semicolon it worked again.Whale
The semicolon causes a syntax error, but triggers a recheck in eclipse, some how after deleting the semicolon then the import is back to normal without the wiggly under line red.. weir weird but WORKS!Nyaya
java.lang.IllegalArgumentException: Page directive: invalid value for importGarwin
A
71

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.

Ailin answered 7/12, 2009 at 8:5 Comment(3)
Thanks. It all looks as you say it ought to be, which is why it so confusing.Ulloa
Kudos, but package is an illegal package identifier ;)Selenite
Of course "Only a type can be imported. XYZ resolves to a package" really means "dependency can't be found in classpath". Of course!Waldenses
U
41

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.eresearch.knowledgeportal.dao.CategoryDao" %>
<%@ page import="org.eresearch.knowledgeportal.model.Category" %>

became

 <%@ page import="java.util.*" %>
 <%@ page import="org.eresearch.knowledgeportal.dao.CategoryDao" %>
 <%@ page import="org.eresearch.knowledgeportal.model.Category;" %>
Ulloa answered 7/12, 2009 at 8:35 Comment(4)
AFAIK adding the semi-colon gives an error and has the same effect as removing the line.Subtenant
This fixed an issue for me. However after I deleted the semicolon it worked again.Whale
The semicolon causes a syntax error, but triggers a recheck in eclipse, some how after deleting the semicolon then the import is back to normal without the wiggly under line red.. weir weird but WORKS!Nyaya
java.lang.IllegalArgumentException: Page directive: invalid value for importGarwin
I
10

If you spell the class name wrong or the class isn't on the classpath, the JSP processor will say it "resolves to a package" rather than that it doesn't exist. This was driving me crazy today as I kept not seeing a typo I'd made.

Indigestion answered 7/1, 2015 at 18:9 Comment(2)
I saw this problem after manually copying class files to my Tomcat webapps/myApp/WEB-INF/classes dir. A bounce of Tomcat fixed it. And I'd guess a redeploy would work as well.Trigraph
Indeed, the message is terrible. Even if they don't want to bother to differentiate the two cases, it should at least have been "class not found or resolves to a package".Granule
P
4

I got this error in Netbeans. As with most bizarre errors like this that appear out of nowhere, I resolve it by going to project properties, changing the Source/Binary Format (doesn't matter to what, just something different), and doing a clean and build.

Philipines answered 26/10, 2012 at 18:50 Comment(3)
Thank you! Wow, can't believe that worked. I will try this next time I have a weird Netbeans error. I don't know why Netbeans misbehaves like this. It happens more now that I installed a new version.Hyetography
Ok, I've had this problem twice now and your trick fixed it both times.Hyetography
I just had this again. I actually resolved it this time by renaming/refactoring the package name that was causing a problem, doing a clean + build, then renaming/refactoring back to what it was, and doing another clean + build. That did the trick as well.Hyetography
S
4

I got it resolved by adding the jars in tomcat lib directory.

Shoal answered 7/9, 2017 at 6:50 Comment(0)
F
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...

Filter answered 7/12, 2009 at 7:51 Comment(0)
T
2

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

Tractile answered 21/11, 2011 at 5:11 Comment(1)
Definitely not. That's the default.Prearrange
A
2

Got this exception as well.

Environment: Mac with Eclipse running Tomcat from inside Eclipse using Servers view.

For any reason Eclipse does not copy classes folder to WEB-INF. After classes folder was manually copied, everything works fine.

Don't know, or it is Eclipse bug or I missed something.

Arcane answered 12/1, 2019 at 11:53 Comment(0)
D
1

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?

Doggo answered 30/11, 2011 at 23:50 Comment(1)
Bingo. In my case, the problem class was in another project which was not copied by Netbeans to the Tomcat lib when changed, as I had expected.Rockel
S
1

My bet is that you have a package called org.ivec.eresearch.knowledgeportal.model.category (small c) and are running on a non-case sensitive filesystem like Windows or Mac. It seems that the compiler gets confused when a class and package exist.

You can either renamed the class "Category" or the package "category" and this error will go away. Unfortunately I'm not sure if this is a Tomcat or ECJ bug.

Sturm answered 1/11, 2012 at 20:43 Comment(1)
Old answer but this just got me on JBoss 7.4. We have an x.y.Utilities class and an x.y.utilties... package. Solution for anyone stumbling upon this in the future using JBoss without having to rename anything is pass -Djboss.vfs.forceCaseSensitive=trueDespair
U
1

generate .class file separate and paste it into relevant package into the workspace. Refresh Project.

Uncork answered 2/5, 2015 at 13:4 Comment(0)
G
1

This typically happens when mixing (in the same jsp page) static jsp import:

<%@include file="...

with dynamic jsp import:

<jsp:include page="...

and your type has been already imported by the "static imported" jsp. When the same type needs to be used (and then imported) by the "dynamically imported" jsp -> this generate the Exception: "Only a type can be imported..."

Groundmass answered 5/7, 2017 at 8:8 Comment(0)
S
0

I had a similar issue. In eclipse I compared my project with a sample project which is working fine (generated by a maven archetype). I found my project has missed 2 lines in /.classpath file. I copied those 2 lines and it fixed the issue. It seems even though I set build path in project preferences, eclipse has not updated accordingly for some reasons.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
    ...
</classpath>
Stonefish answered 26/5, 2012 at 20:29 Comment(0)
C
0

My contribution: I got this error because I created a package named 3lp. However, according to java spec, you are not allowed to name your package starts with a number. I changed it to _3lp, now it works.

Chanticleer answered 21/9, 2012 at 21:29 Comment(0)
O
0

If you are using Maven and packaging your Java classes as JAR, then make sure that JAR is up to date. Still assuming that JAR is in your classpath of course.

Occlusive answered 16/4, 2013 at 15:42 Comment(0)
S
0

I resolved it by adding the jar file containing the imported classes into WEB-INF/Lib.

Stannfield answered 22/4, 2014 at 10:34 Comment(0)
S
0

Are you attempting to import an overridden class like I was?

If so, your overridden class is in the wrong package, or simply non-existent.

Creating, or moving the class into the correct location (src/[package.package].[class]) could solve your problem.

Suburb answered 2/9, 2014 at 22:42 Comment(0)
B
0

To me it was a wrong deployment. Deployed properly, everything works (check my question for details).

Bina answered 11/9, 2016 at 21:26 Comment(1)
It looks like wrong deployment is the main reason of this issue, redeploying worked for me.Rennin
P
0

So I had the same issue and just wanted to give my solution. Maybe someone faces the same problem.

I had properly configured my artifact but somehow intellij mixed something up and deleted the WEB-INF folder in one of my artifacts.

When I viewed the contents of my .war file every class was present in the correct folder structure. So I didn't think that anything was missing. But when I check the artifacts in Intellij and compared it to a working artifact then I realized, that the WEB-INF folder with the classes and libs were not present.

Add them to the artifact and it should work.

TLDR: Intellij deleted the WEB-INF Folder in my .war artifact. Just check if yours is missing aswell. (Project Strucutre -> Artifacts)

Potbellied answered 3/1, 2019 at 14:25 Comment(0)
B
0

The tomcat folder structure has Webapps/ROOT/ inside where the WEB-INF folder has to be present. If you place your WEB-INF inside Webapps/ tomcat is not locating the class files and jars.

Burg answered 29/10, 2019 at 6:8 Comment(0)
S
0

Just adding yet another way of achieving this weird error

To me what happened is that the classpath had a few jars from tomcat by using the TOMCAT_HOME variable.

I didn't have that variable set and never needed it in over a year working in my current job. But this specific project was expecting this variable in the classpath and Eclipse wouldn't tell me there was a classpath error going on.

Sarcophagus answered 31/7, 2020 at 14:13 Comment(0)
W
0

I was getting the same error and nothing was wrong with my java files and packages. Later I noticed that the folder name WEB-INF was written like this "WEB_INF". So correcting just the folder name solved the issue

Wrist answered 2/9, 2020 at 14:52 Comment(0)
A
0

I had this same issue when running from Eclipse. To fix the issue I went into the Configure Build Path and removed the src folder from the build path. Then closed and reopened the project. Then I added the src folder to the build path. The src folder contained the java class I was trying to access.

Ametropia answered 8/11, 2020 at 4:42 Comment(0)
S
0

This plagued us on Tomcat 9.0.62. It turns out our issue was as simple as permissions. One class was working, but another wasn't (due to permissions):

/WEB-INF/classes/ listing:

drwxr-xr-x. 3 tomcat tomcat 4096 Dec  1 08:52 working_class_directory
drw-r--r--. 2 tomcat tomcat 4096 Dec  1 10:14 non_working_class_directory

A simple "chmod 755 non_working_class_directory" command in the /WEB-INF/classes/ directory fixed the issue.

I hope that helps someone.

Sike answered 1/12, 2023 at 19:4 Comment(0)
L
-1

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;
Leatherman answered 7/12, 2009 at 7:46 Comment(0)
S
-1

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?

Selfexpression answered 7/12, 2009 at 7:49 Comment(0)
M
-1

I had the same error message and my way to deal with it is as follows:

  1. First go check the file directory where your Tomcat is publishing your web application, e.g. D:\Java\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\myDatatable\WEB-INF\classes, in which we normally put our classes. If this is not the place where you put your classes, then you have to find out where by default it is by right click your web application root name-->Build Path-->Configure Build Bath...-->Then check the "Source" Tab and find out the field value of "Default output folder". This shall be the place where Tomcat put your classes.
  2. You would see that XYZ class is not yet built. In order to build it, you could go to Menu "Project"-->"Clean..."-->Select your web application to clean.
  3. After it's completed, try restart your tomcat server and go check the file directory again. Your class should be there. At least it works for me. Hope it helps.
Measured answered 27/12, 2016 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.