2424 * b) We can't modify java.library.path, not in an official, forward-compatible manner..
2525 */
2626public class NativeLoader {
27+ private static String TAG = "NativeLoader" ;
2728 private static String libraryDir = null ;
2829 private static Set <String > loadedLibraries = new HashSet <>();
2930
@@ -48,10 +49,9 @@ public static void loadLibrary(String libraryName) {
4849 }
4950
5051 String sourceLibraryDir = String .format ("natives/%s-%s" , osName , arch );
51- System . out . println ( "sourcelibrarydir " + sourceLibraryDir );
52+ File tempDir = createTempDirectory ( osName . equals ( "windows" ) );
5253
53- File tempDir = createTempDirectory ();
54- System .out .println ("created temp dir, path is " +tempDir );
54+ System .out .println (TAG +": extracting native libraries to " +tempDir );
5555
5656 extract (sourceLibraryDir , tempDir );
5757
@@ -86,18 +86,26 @@ public static void loadLibrary(String libraryName) {
8686 loadedLibraries .add (libraryName );
8787 }
8888
89- private static File createTempDirectory () {
90- File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "freej2me-" + System .nanoTime ());
91- if (!tempDir .mkdir ()) {
89+ private static File createTempDirectory (boolean isWindows ) {
90+ // on Windows, we can't delete the directory on exit because the dll files
91+ // are still loaded.. so we use a persistent location to avoid flooding the temp directory
92+
93+ String dirName = isWindows ? "freej2me-natives-tmp" : "freej2me-" + System .nanoTime ();
94+
95+ File tempDir = new File (System .getProperty ("java.io.tmpdir" ), dirName );
96+ if (!tempDir .exists () && !tempDir .mkdir ()) {
9297 throw new RuntimeException ("Failed to create a temporary directory" );
9398 }
94- Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
95- try {
96- NativeLoader .deleteDirectory (tempDir );
97- } catch (Exception e ) {
98- e .printStackTrace ();
99- }
100- }));
99+
100+ if (!isWindows ) {
101+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
102+ try {
103+ NativeLoader .deleteDirectory (tempDir );
104+ } catch (Exception e ) {
105+ e .printStackTrace ();
106+ }
107+ }));
108+ }
101109 return tempDir ;
102110 }
103111
@@ -129,13 +137,23 @@ private static void extract(String directoryToExtract, File tempDir) {
129137 Files .createDirectories (entryDest );
130138 } else {
131139 Files .createDirectories (entryDest .getParent ());
140+
132141 try (InputStream is = jar .getInputStream (entry );
133142 OutputStream os = Files .newOutputStream (entryDest )) {
134143 byte [] buffer = new byte [4096 ];
135144 int bytesRead ;
136145 while ((bytesRead = is .read (buffer )) != -1 ) {
137146 os .write (buffer , 0 , bytesRead );
138147 }
148+ } catch (FileSystemException e ) {
149+ // on Windows this might fail when there's another
150+ // instance of freej2me running.
151+ // so if the file already exists, let it be.
152+ if (!Files .exists (entryDest )) {
153+ throw e ;
154+ } else {
155+ System .out .println (TAG +": assuming another instance is running" );
156+ }
139157 }
140158 }
141159 }
0 commit comments