I launch a simple WEB application on java using tomcat and servlets building with maven, and using a class that uses third-party libraries org.apache.poi throws a NoClassDefFoundError error: org / apache / poi / xssf / usermodel / XSSFWorkbook. when replacing and using other classes throws an error on them. In tests, everything works and finds all dependencies. And through the servlets on the site is nothing. How to write dependencies in the poi file or where to put them so that the web application can see them or they are uploaded via maven to the web project. Actually files: pom.xml dependencies:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yurets_y</groupId> <artifactId>utl2-plan-chart</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <dependencies> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans --> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> </dependencies> </project> list.jsp - in which problems occur
<%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <div> <div> <form method="post"> <input list="stationList" name="stationList"> <datalist id="stationList"> <option value="Station one"> <% List<String> names = (List<String>) request.getAttribute("stationList"); for (String s : names) { out.println("<option value=" + s + ">"); } %> </datalist> </form> </div> </body> </html> The servlet itself that accesses the database file:
package app.servlets; import app.model.ChartModel; import app.model.Model; import javax.servlet.RequestDispatcher; import java.io.IOException; import java.util.List; public class AddUserServlet extends javax.servlet.http.HttpServlet { protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { } protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { ChartModel model = ChartModel.getInstance(); List<String> names = model.getStationNames(); request.setAttribute("userNames",names); RequestDispatcher requestDispatcher = request.getRequestDispatcher("views/add.jsp"); requestDispatcher.forward(request, response); } }
And actually java class itself, which uses third-party libraries:
package app.model; import app.model.bin.Station; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Paths; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ChartModel { private static ChartModel ourInstance = new ChartModel(); private Set<Station> stationSet; public static ChartModel getInstance() { return ourInstance; } private ChartModel() { stationSet = new LinkedHashSet<>(); URL url = ChartModel.class.getResource("/datares/stationsUZ.xlsx"); File file = null; try { file = Paths.get(url.toURI()).toFile(); } catch (URISyntaxException e) { e.printStackTrace(); } if(file.exists()){ try { stationSet = loadStationsFromExcel(file); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } else{ System.out.println("Не удалось загрузить данные из списка!"); } } private Set<Station> loadStationsFromExcel(File file) throws IOException, Exception { Set<Station> stations = new LinkedHashSet<>(); XSSFWorkbook myExcelBook = new XSSFWorkbook(file); XSSFSheet myExcelSheet = myExcelBook.getSheetAt(0); int rowIndex = 1; XSSFRow row; Station station; do { row = myExcelSheet.getRow(rowIndex++); if (row == null) { break; } station = getStationFromRow(row); if (station != null) { stations.add(station); }else break; } while (true); myExcelBook.close(); return stations; } private Station getStationFromRow(XSSFRow row) { int code; String rusName; String ukrName; String administration; String railDepartment; int node; if (row.getCell(0) == null) { return null; } int i = 0; code = (int)row.getCell(i++).getNumericCellValue(); rusName = row.getCell(i++).getStringCellValue(); ukrName = row.getCell(i++).getStringCellValue(); administration = row.getCell(i++).getStringCellValue(); railDepartment = row.getCell(i++).getStringCellValue(); node = (int) row.getCell(i).getNumericCellValue(); return new Station(code, rusName, ukrName, administration, railDepartment,node); } public Set<Station> getStationSet() { return stationSet; } public List<String> getStationNames(){ return stationSet .stream() .map(Station::toString) .collect(Collectors.toList()); } public List<String> getFakeList(){ List<Station> model = new ArrayList<>(); model.add(new Station(1,"Первая","Перша","УЗ","ЮЗЖД",1)); model.add(new Station(2,"Вторая","Друга","УЗ","ЮЗЖД",1)); model.add(new Station(3,"Третья","Третя","УЗ","ЮЗЖД",1)); model.add(new Station(4,"Четвертая","Четверта","УЗ","ЮЗЖД",1)); model.add(new Station(5,"Пятая","Пята","УЗ","ЮЗЖД",1)); return model.stream() .map(Station::toString) .collect(Collectors.toList()); } }
provided, which implies that Tomcat servlet containers are present (for example, in $ CATALINA_HOME / lib or $ CATALINA_BASE / lib folders). Right now, I don’t remember where it is better to put a jarnik. If you are building a project as war, you can remove the<scope>provided</scope>from pom.xml from the dependency and dig in the direction of adding the dependency to war-nick. - gooamoko pm