Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK9+下,以jar方式启动支持ClassPath #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/main/java/io/xjar/jar/XJarLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.net.URLClassLoader;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -46,9 +48,22 @@ public void launch() throws Exception {
if (path == null) {
throw new IllegalStateException("Unable to determine code source archive");
}
File jar = new File(path);
URL url = jar.toURI().toURL();
xJarClassLoader = new XJarClassLoader(new URL[]{url}, classLoader.getParent(), xLauncher.xDecryptor, xLauncher.xEncryptor, xLauncher.xKey);
String classPath = getJavaClassPath();
if (null == classPath || classPath.isEmpty()) {
File jar = new File(path);
URL url = jar.toURI().toURL();
xJarClassLoader = new XJarClassLoader(new URL[]{url}, classLoader.getParent(), xLauncher.xDecryptor, xLauncher.xEncryptor, xLauncher.xKey);
} else {
List<String> jarPathList = analyzeClassPath(classPath);
URL[] urlArray = new URL[jarPathList.size()];
for (int i = 0; i < jarPathList.size(); i++) {
String single = jarPathList.get(i);
File classPathJar = new File(single);
URL classPathUrl = classPathJar.toURI().toURL();
urlArray[i] = classPathUrl;
}
xJarClassLoader = new XJarClassLoader(urlArray, classLoader.getParent(), xLauncher.xDecryptor, xLauncher.xEncryptor, xLauncher.xKey);
}
}

Thread.currentThread().setContextClassLoader(xJarClassLoader);
Expand All @@ -66,4 +81,22 @@ public void launch() throws Exception {
mainMethod.invoke(null, new Object[]{xLauncher.args});
}

private List<String> analyzeClassPath(String classPath) {
List<String> jarPathList = new ArrayList<>();
String[] jarPathArryay = classPath.split(File.pathSeparator);
if (jarPathArryay.length == 0) {
return jarPathList;
}
for (String jarPath : jarPathArryay) {
if (null == jarPath || jarPath.trim().isEmpty()) {
continue;
}
jarPathList.add(jarPath.trim());
}
return jarPathList;
}

private String getJavaClassPath() {
return System.getProperty("java.class.path");
}
}