4
4
import org .springframework .boot .SpringApplication ;
5
5
import org .springframework .context .ConfigurableApplicationContext ;
6
6
7
- import java .io .IOException ;
8
7
import java .net .URL ;
9
8
import java .net .URLClassLoader ;
10
9
import java .nio .file .Files ;
11
10
import java .nio .file .Path ;
12
11
import java .nio .file .Paths ;
13
12
import java .util .ArrayList ;
14
13
import java .util .List ;
14
+ import java .util .stream .Stream ;
15
15
16
16
public class DynamicApplication {
17
17
@@ -40,6 +40,7 @@ public void run0(Class<?> applicationClass, String[] args) {
40
40
41
41
42
42
public static void run (Class <?> applicationClass , String [] args ) {
43
+ DynamicApplication .getInstance ().addDynamicJars ();
43
44
DynamicApplication .getInstance ().run0 (applicationClass , args );
44
45
}
45
46
@@ -50,31 +51,29 @@ public static void restart() {
50
51
public void restart0 () {
51
52
Thread thread = new Thread (() -> {
52
53
context .close ();
53
- try {
54
- this .addDynamicJars ();
55
- } catch (IOException e ) {
56
- throw new RuntimeException (e );
57
- }
54
+ this .addDynamicJars ();
58
55
this .run0 (applicationClass , runArgs );
59
56
});
60
57
thread .setDaemon (false );
61
58
thread .start ();
62
59
}
63
60
64
- private void addDynamicJars () throws IOException {
61
+ private void addDynamicJars (){
65
62
Path libsPath = Paths .get (jarsFolder );
66
63
if (Files .exists (libsPath ) && Files .isDirectory (libsPath )) {
67
64
System .out .println ("Start Load Dynamic Jars:" );
68
65
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 ){}
78
77
URL [] urls = jarUrls .toArray (new URL [0 ]);
79
78
URLClassLoader urlClassLoader = new URLClassLoader (urls , ClassLoader .getSystemClassLoader ());
80
79
Thread .currentThread ().setContextClassLoader (urlClassLoader );
0 commit comments