File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The import cannot be resolved

 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any jar file you want a web app to use needs to be inside of that web app's WEB-INF/lib folder.
 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you certain that there isn't a typo in the import directive?
 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A typo is a spelling mistake: http://en.wikipedia.org/wiki/Typographical_error
 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to know - could you explain why ?
thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the layout and contents of the WEB-INF/classes and WEB-INF/lib directories.
 
moshi cochem
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic