44import org .springframework .boot .SpringApplication ;
55import org .springframework .context .ConfigurableApplicationContext ;
66
7- import java .io .IOException ;
87import java .net .URL ;
98import java .net .URLClassLoader ;
109import java .nio .file .Files ;
1110import java .nio .file .Path ;
1211import java .nio .file .Paths ;
1312import java .util .ArrayList ;
1413import java .util .List ;
14+ import java .util .stream .Stream ;
1515
1616public class DynamicApplication {
1717
@@ -40,6 +40,7 @@ public void run0(Class<?> applicationClass, String[] args) {
4040
4141
4242 public static void run (Class <?> applicationClass , String [] args ) {
43+ DynamicApplication .getInstance ().addDynamicJars ();
4344 DynamicApplication .getInstance ().run0 (applicationClass , args );
4445 }
4546
@@ -50,31 +51,29 @@ public static void restart() {
5051 public void restart0 () {
5152 Thread thread = new Thread (() -> {
5253 context .close ();
53- try {
54- this .addDynamicJars ();
55- } catch (IOException e ) {
56- throw new RuntimeException (e );
57- }
54+ this .addDynamicJars ();
5855 this .run0 (applicationClass , runArgs );
5956 });
6057 thread .setDaemon (false );
6158 thread .start ();
6259 }
6360
64- private void addDynamicJars () throws IOException {
61+ private void addDynamicJars (){
6562 Path libsPath = Paths .get (jarsFolder );
6663 if (Files .exists (libsPath ) && Files .isDirectory (libsPath )) {
6764 System .out .println ("Start Load Dynamic Jars:" );
6865 List <URL > jarUrls = new ArrayList <>();
69- Files .list (libsPath )
70- .filter (file -> file .toString ().endsWith (".jar" ))
71- .forEach (file -> {
72- try {
73- URL url = file .toUri ().toURL ();
74- jarUrls .add (url );
75- System .out .println (url );
76- } catch (Exception ignored ) {}
77- });
66+ try (Stream <Path > stream = Files .list (libsPath )){
67+ stream
68+ .filter (file -> file .toString ().endsWith (".jar" ))
69+ .map (Path ::toUri )
70+ .forEach (uri -> {
71+ try {
72+ jarUrls .add (uri .toURL ());
73+ System .out .println (uri );
74+ } catch (Exception ignored ) {}
75+ });
76+ }catch (Exception ignored ){}
7877 URL [] urls = jarUrls .toArray (new URL [0 ]);
7978 URLClassLoader urlClassLoader = new URLClassLoader (urls , ClassLoader .getSystemClassLoader ());
8079 Thread .currentThread ().setContextClassLoader (urlClassLoader );
0 commit comments