ClassNotFoundException in Maven project (commons.fileupload)

Answered

Class [ org/apache/commons/fileupload/FileItemFactory ] not found. Error while loading [ class main.java.servlets.Register ]]]

I'm getting this warning when running this project and I get the relevant ClassNotFoundException when I actually send the request to the servlet.  All the jar file are there.But I'm still getting this. This happens only with commons.fileupload library . But the same library works well in NetBeans without exceptions.


0
6 comments

Make sure the jar is included in the artifact that you deploy on the server, see https://www.jetbrains.com/help/idea/artifacts.html.

1
Avatar
Permanently deleted user

Yes the jar is included in the artifact but I still get the exception.

0

Glassfish comes with fileupload library of a different version, could be a conflict so that the older version is already loaded by the server and has the priority.

Try using the same version as comes with your Glassfish installation or try a new Glassfish server version.

http://stackoverflow.com/help/mcve would be required to investigate further.

1
Avatar
Permanently deleted user

But on NetBeans ,this works fine in the same GlassFish version and the same library version.

Can you tell me the steps I should take when I'm getting ClassNotFoundExceptions from jars of a Maven project that has the dependencies imported properly. 

The dependency scope is set to compile but I don't think the compiler is finding them during compilation. I even get suggestions in the editor for the exact same classes I'm getting exceptions for. This is the servlet. 

 


package servlets;

import hibernate.NewHibernateUtil;
import hibernate.User;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.hibernate.Session;
import org.hibernate.Transaction;


@WebServlet(name = "Register", urlPatterns = {"/Register"})
public class Register extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Session s = NewHibernateUtil.getSessionFactory().openSession();

User b = new User();

DiskFileItemFactory dff = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(dff);

List<FileItem> items = sfu.parseRequest(req);
for (FileItem i : items) {
if (i.isFormField()) {

if (i.getFieldName().equals("name")) {
b.setName(i.getString());
} else if (i.getFieldName().equals("email")) {
b.setEmail(i.getString());
} else if (i.getFieldName().equals("passwd")) {
b.setPassword(i.getString());
} else if (i.getFieldName().equals("mobile")) {
b.setMobile(i.getString());
}
else if (i.getFieldName().equals("country")) {
b.setCountry(i.getString());
}
else{
b.setType(i.getString());
}

} else {
String path = req.getServletContext().getRealPath("/");
File f = new File(path + "images/profile_pic"+b.getName()+".png");
i.write(f);
b.setImgUrl("images/profile_pic"+b.getName()+".png");
}

}
s.save(b);
Transaction t = s.beginTransaction();
t.commit();
resp.getWriter().write("Success");
} catch (Exception e) {
e.printStackTrace();
}
}

}

0

Please share the complete project to reproduce the problem: https://intellij-support.jetbrains.com/hc/articles/206869619.

0
Avatar
Permanently deleted user

Thank you . I have not build the project properly. It's fixed now. 

0

Please sign in to leave a comment.