Pierre
Pierre

Reputation: 5156

Java/JSP WEB-INF/classes can't import

Its been a while since I had to do some Java/JSP...

I have a java class in WEB-INF/classes/MyClass.java The build in Netbeans is successful and I can see MyClass.class in the classes folder.

In my jsp page, I've got

<%@ page import="MyClass" %>

Tomcat says that the import cannot be resolved...

I tried to put MyClass in a package(WEB-INF/com/MyClass) and then import the package into my Jsp file. The import doesnt throw an error anymore then, but I cannot instanciate a MyClass object, says the type is unresolved...

What am I doing wrong here..?

All help appreciated! :)

Upvotes: 3

Views: 13333

Answers (4)

Pierre
Pierre

Reputation: 5156

OMG, I found my mistake...

Netbeans wasn't copying the lib files to the right folder, my jsp page was being updated, so it looked like all the files were copied, but actually MyClass.class wasnt in the folder...

Thanks for your help!

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

.class file must be placed inside the classes folder under WEB-INF. So, the location of MyClass.class must be WEB-INF/classes/com/ (In case com is a package).

<%
 // Instantiate a MyClass

 com.MyClass obj=new com.MyClass();

%>

OR

<%@ page import="com.MyClass" %>
<%
   MyClass obj=new MyClass();
%>

Upvotes: 3

djna
djna

Reputation: 55957

What package is MyClass in? If the default package then you can put the class file in

WEB-INF/classes

if it's in a package, then use the package directory hierarchy under classes

Upvotes: 0

KB22
KB22

Reputation: 6979

WEB-INF/classes/MyClass.java 

makes me guess that you're using the default package which is'nt a good practice at all. Try assigning your class to a package and do the import according to that.

Do something like:

package myPackage;

class myClass
{
...
}

And afterwards:

<%@ page import="myPackage.myClass" %>

Upvotes: 4

Related Questions