One of the problems of starting tomcat in idea

Recently, a simple springmvc project was newly created, which is also managed by maven. After everything is configured, I started to use tomcat in the idea but kept reporting an error. I checked the main log here:

java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
	at org.springframework.util.Log4jConfigurer.shutdownLogging(Log4jConfigurer.java:123)
	at org.springframework.web.util.Log4jWebConfigurer.shutdownLogging(Log4jWebConfigurer.java:172)
	at org.springframework.web.util.Log4jConfigListener.contextDestroyed(Log4jConfigListener.java:54)
	at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4840)

Caused by: java.lang.ClassNotFoundException: org.apache.log4j.LogManager
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
	... 49 more

 

Investigation process:

1 According to the prompt, it is obvious that the corresponding jar package is missing, and then go to the pom file to see it. Several jars related to this log exist:

       <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-version}</version>
        </dependency>

     ps: slf4j version number above: 1.7.12 

      

     2 Since there is no problem with the jar, but the tomcat startup has been reporting an error. So I don't need tomcat to start it first, but start it directly in the idea. The method is to use unit tests to test.

 

    ①Relevant dependency packages:

<!--test-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.8.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-version}</version>
        </dependency>

   

   ② Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml","/dispatcher-servlet.xml"})
public class TxtFileServiceImplTest {
    @Autowired
    private TxtFileService txtFileService;
   
    public void testStart(){
        txtFileService.getLocalTxtFileVale();
    }
}

    The result can be successfully tested normally, which proves that the project is no problem.

 

   3 Check the jar package packaged into tomcat (under tomcat-webapps-project name-lib) It turns out that these packages are missing, and the problem is found. It turns out that the package was not added when the tomcat startup project was configured. The solution:

   1⃣️ Select the item with the mouse and press F4 or right-click to select open-moulde-setting 

   2⃣️ Select Artifacts on the left side of the pop-up box and then select the war package that your tomcat parses. In the largest box on the right side, check whether the package you need to load has been placed in the WEB-INF--lib directory, if not , select the package to be loaded, right-click, and select Put into web-inf/lib. As shown below:

 

   

   Of course, under normal circumstances, creating a project will be added by default.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326338906&siteId=291194637