Jetty ClassLoader problem

Background

In simple terms, it is to use intellij to start the project with embedded jetty, and class not found exception , but there is no problem starting with eclipse, and it has been started in eclipse before. Recently, this problem appeared when I switched to intellij. The following is the specific configuration

configuration

Development environment

Development tool intellij
Project embedded jetty+jFinal

Problem
I started using intellij and failed. The project used to be started in eclipse, but there was no problem at all. Recently, intellij has become popular before switching to intellij.

Code configuration

public class MyJFinalConfig extends JFinalConfig{ public static void main(String[] args) {JFinal.start("WebRoot", 8080, "/myproject", 5 ); }}

web.xml configuration

<filter> <filter-name>jfinalfilter-name> <filter -class>com.jfinal.core.JFinalFilterfilter-clas s> param> <param-name>configClassparam-name> <param-value>com.mypak.MyJFinalConfig< /param-value> param span>> filter>

Exception informationclass not found com.mypak.MyJFinalConfig

< h1 id="Analysis">Analysis

Tracking the source code of the error

ps: The code is basically like this, just to express that meaning.

Object obj = Class.forName("com.mypak.MyJFinalConfig" ).forInstance;if(!obj instanceof JFinalFilter){ throw new Exception("Something went wrong")}

classloader is tricking ghosts

We know that MyJFinalConfig is a subclass of JFinalFilter, and its instance must be JFinalFilter instance, but according to the actual running result, it is not!
The first reaction thought may be that ClassLoader is playing tricks, because When the ClassLoader is different, the relationship between the upper and lower levels of the class is impossible to talk about, and it is the jetty built-in startup, which is different from In the past, it started normally, so it might be related to the classloader. Debug found that the MyJFinalConfig class loader is sun.misc.Launcher$AppClassLoader, and JFinalFilter is the WebAppClassLoader in the jetty package. code>, as expected, the classloader is different! The reason was found.

Why can eclipse work but not Intellij?

After searching the article, I found that jetty’s classloader will treat the classes under WEB-INF/lib and WEB-INF/classes as the app’s class instead of entrusting it to the parent classloader. Based on this, set the outputpath of intellij to webRoot/WEB-INF/classes and let WebAppClassLoader load MyJFinalConfig so that both classes are WebAppClassLoader NS.

Amendment

Set outputpath to exclude resource files under webRoot, otherwise there are resources files such as pages in the classes folder
D:\intellij_workspace\ myproject\WebRoot\WEB-INF\classes.
Write the picture description here

Write picture description here

follow-up

When I read the JFinal manual later, I found the same reasoning A sentence. The Default out folder here must be exactly the same as the WebRoot\WEB-INF\classes directory to start the project with JFinal integrated Jetty
Write the picture description here

div>

Background

Simply put, it is to use intellij to start the project with the embedded jetty, bursting with class not found exception

code>, but there is no problem starting with eclipse, and it has been started in eclipse before. Recently, this problem appeared when I switched to intellij. The following is the specific configuration

configuration

Development environment

Development tool intellij
Project embedded jetty+jFinal

Problem
I started using intellij and failed. The project used to be started in eclipse, but there was no problem at all. Recently, intellij has become popular before switching to intellij.

Code configuration

public class MyJFinalConfig extends JFinalConfig{ public static void main(String[] args) {JFinal.start("WebRoot", 8080, "/myproject", 5 ); }}

web.xml configuration

<filter> <filter-name>jfinalfilter-name> <filter -class>com.jfinal.core.JFinalFilterfilter-class> param> <param-name>configClassparam-name> <param-value>com.mypak.MyJFinalConfigparam-value> param > filter>

Exception informationclass not found com.mypak.MyJFinalConfig

Analysis

Tracing the source code of the error

ps: The code is basically like this, just to express that meaning.

Object obj = Class.forName("com.mypak.MyJFinalConfig" ).forInstance;if(!obj instanceof JFinalFilter){ throw new Exception("Something went wrong")}

classloader is tricking ghosts

We know that MyJFinalConfig is a subclass of JFinalFilter, and its instance must be JFinalFilter instance, but according to the actual running result, it is not!
The first reaction thought may be that ClassLoader is playing tricks, because When the ClassLoader is different, the relationship between the upper and lower levels of the class is impossible to talk about, and it is the jetty built-in startup, which is different from In the past, it started normally, so it might be related to the classloader. Debug found that the MyJFinalConfig class loader is sun.misc.Launcher$AppClassLoader, and JFinalFilter is the WebAppClassLoader in the jetty package. code>, as expected, the classloader is different! The reason was found.

Why can eclipse work but not Intellij?

After searching the article, I found that jetty’s classloader will treat the classes under WEB-INF/lib and WEB-INF/classes as the app’s class instead of entrusting it to the parent classloader. Based on this, set the outputpath of intellij to webRoot/WEB-INF/classes and let WebAppClassLoader load MyJFinalConfig so that both classes are WebAppClassLoader NS.

Amendment

Set outputpath to exclude resource files under webRoot, otherwise there are resources files such as pages in the classes folder
D:\intellij_workspace\ myproject\WebRoot\WEB-INF\classes.
Write the picture description here

Write picture description here

follow-up

When I read the JFinal manual later, I found the same reasoning A sentence. The Default out folder here must be exactly the same as the WebRoot\WEB-INF\classes directory before you can use JFinal integrated Jetty to start the project
Write the picture description here

p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 476 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.