Skip to content

Commit

Permalink
Merge pull request #55 from imario42/master
Browse files Browse the repository at this point in the history
alternative classloader strategy
  • Loading branch information
Edvin Syse authored Feb 22, 2017
2 parents 1da4ce1 + d19f39e commit ce53a4b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
68 changes: 68 additions & 0 deletions src/main/java/fxlauncher/FxlauncherClassCloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package fxlauncher;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

/**
* Created by im on 22.02.17.
*/
public class FxlauncherClassCloader extends URLClassLoader
{
public FxlauncherClassCloader(ClassLoader parentClassLoader)
{
super(buildClasspath(System.getProperty("java.class.path")), parentClassLoader);
}

void addUrls(List<URL> urls)
{
for (URL url : urls)
{
this.addURL(url);
}
}

private static URL[] buildClasspath(String classPath)
{
if (classPath == null || classPath.trim().length() < 1)
{
return new URL[0];
}

List<URL> urls = new ArrayList<>();

int pos;
while ((pos = classPath.indexOf(File.pathSeparatorChar)) > -1)
{
String part = classPath.substring(0, pos);

addClasspathPart(urls, part);

classPath = classPath.substring(pos + 1);
}

addClasspathPart(urls, classPath);

return urls.toArray(new URL[urls.size()]);
}

private static void addClasspathPart(List<URL> urls, String part)
{
if (part == null || part.trim().length() < 1)
{
return;
}

try
{
urls.add(new File(part).toURI().toURL());
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
}
23 changes: 17 additions & 6 deletions src/main/java/fxlauncher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,24 @@ private void createUpdateWrapper() {
});
}

private URLClassLoader createClassLoader(Path cacheDir) {
private ClassLoader createClassLoader(Path cacheDir) {
List<URL> libs = manifest.files.stream().filter(LibraryFile::loadForCurrentPlatform).map(it -> it.toURL(cacheDir)).collect(Collectors.toList());

return new URLClassLoader(libs.toArray(new URL[libs.size()]));
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
if (systemClassLoader instanceof FxlauncherClassCloader)
{
((FxlauncherClassCloader) systemClassLoader).addUrls(libs);
return systemClassLoader;
}
else
{
ClassLoader classLoader = new URLClassLoader(libs.toArray(new URL[libs.size()]));
FXMLLoader.setDefaultClassLoader(classLoader);
Thread.currentThread().setContextClassLoader(classLoader);
Platform.runLater(() -> Thread.currentThread().setContextClassLoader(classLoader));

return classLoader;
}
}

private void launchAppFromManifest(boolean showWhatsnew) throws Exception {
Expand Down Expand Up @@ -247,10 +261,7 @@ private void createApplication() throws Exception {

Path cacheDir = manifest.resolveCacheDir(getParameters() != null ? getParameters().getNamed() : null);

URLClassLoader classLoader = createClassLoader(cacheDir);
FXMLLoader.setDefaultClassLoader(classLoader);
Thread.currentThread().setContextClassLoader(classLoader);
Platform.runLater(() -> Thread.currentThread().setContextClassLoader(classLoader));
ClassLoader classLoader = createClassLoader(cacheDir);
Class<? extends Application> appclass = (Class<? extends Application>) classLoader.loadClass(manifest.launchClass);

PlatformImpl.runAndWait(() ->
Expand Down

0 comments on commit ce53a4b

Please sign in to comment.