wood burning stoves 2.0*
The moose likes Java in General and the fly likes The import cannot be resolved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "The import cannot be resolved" Watch "The import cannot be resolved" New topic
Author

The import cannot be resolved

moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Hi there,
I develope a jsp using eclipse, and try to call a jar file that I made.
The problem is that when I import the jar into the jsp page, I get an error says:
'The import myClass cannot be resolved'.
I included the jar in the Libraries in the build path. The class I'm trying to use in the jar is public.
I also read that running a 'clean' action (project -> clean) can solve it, and I did it. It didn't solve
my problem.
I still get The import cannot be resolved error.
I'll be glad to here any idea, I didn't find a solution yet :-(
Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Any jar file you want a web app to use needs to be inside of that web app's WEB-INF/lib folder.


Ping & DNS - my free Android networking tools app
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Hi, thanks for your advice.
Well, that is what I put my jar inside the lib in the web project, but it is still not working:
Actually, the jar contains some classes that some of them are recognizad in the "import",
but the class that I built and want to use is not recognized.
I don't know why, driving me crazy !
The both classes are public.
Thanks for any help
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Are you certain that there isn't a typo in the import directive?
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
I'm sorry , I'm not sure what is a typo in the import directive....
if you could explain where can I see that.
thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
A typo is a spelling mistake: http://en.wikipedia.org/wiki/Typographical_error
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
ok, so I tried it again an now I put my class in import:
<%@page import="hasp_demo"%>
and I can use it, I did:
document.write(<%=hasp_demo.main(new String[1]) %>); just for a test - it autocompletes the methods in hasp_demo,
so I understand that it recognizes this class.
the problem is that in the import line, it red underlines the "hasp_demo" and when I pass the mouse over the red line, it
writes: "The import hasp_demo cannot be resolved". Moreover - when I run my jsp file, I get exception:

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

An error occurred at line: 6 in the generated java file
The import hasp_demo cannot be resolved

An error occurred at line: 11 in the jsp file: /test.jsp
hasp_demo cannot be resolved
8: <title>Insert title here</title>
9: </head>
10: <body>
11: document.write(<%=hasp_demo.main(new String[1]) %>);
12: cat
13: </body>
14: </html>


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

I hope that I suplied all the required data to help me.
Thanks a lot.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
You should always use classes that are in packages; don't use the default package (i.e, no package), especially not in JSPs.
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
just to know - could you explain why ?
thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
The short explanation is "because classes that are not in packages don't work as JSP beans (and in some other contexts)", the longer explanations have to do with code maintenance reasons (like polluting the global namespace) and some arcane JVM internals.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You cannot import classes that are in the default package; this doesn't even work in standard Java (i.e. not in a JSP). If you try something like this:\

then you will get a compiler error.

(Note: This was possible in older versions of Java, but Sun removed this in Java 5 - probably it was never meant to be possible).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Good morning guys, I'm back :-)
Well, I created again my jar, but now - as you suggested - I put my class inside of a new Package. (Not the default package, but a new one).
The problem is that I can't run it in console - I write: java -jar myJar.jar (like I did before - when the class was out of package), and I get:
Exception in thread "main" java.lang.NoClassDefFoundError....
Any idea ?
Tnks !
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Ok ,
so write now it seem that my class is recognized !
BUT.... when I run my jsp, I get an exception that I have no clue what it says:
The server encountered an internal error () that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: Hasp/hasp_demo
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: Hasp/hasp_demo
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.test_jsp._jspService(test_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause
java.lang.NoClassDefFoundError: Hasp/hasp_demo
org.apache.jsp.test_jsp._jspService(test_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Could someone help me with it ?
TNKS
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Post the layout and contents of the WEB-INF/classes and WEB-INF/lib directories.
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
Hi , thanks for your reply.
I changed the class inside the jar, to start with capital later. I thought that it has something to do with it.
my class: HaspDemo is inside the package: Hasp.
my code inside the jsp is:

<%@page import="Hasp.HaspDemo"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%= HaspDemo.CheckFlow() %>
</body>
</html>

(CheckFlow() is a static method).

The error I get is:

An error occurred at line: 6 in the generated java file
Only a type can be imported. Hasp.HaspDemo resolves to a package

An error occurred at line: 16 in the jsp file: /test.jsp
HaspDemo cannot be resolved

13: <title>Insert title here</title>
14: </head>
15: <body>
16: <%= HaspDemo.CheckFlow() %>
17:
18: </body>
19: </html>

inside the lib I have the jar that I created.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The import cannot be resolved
 
Similar Threads
JSP page not displayed - javax.servlet.jsp.PageContext could not be resolved
How to intergrate Apache CXF in JBoss 4.0
Getting contact list from gmail
JSP strange problem
mail sending in jsp