Learn to work when run via jar instead of via .class file directly
This commit is contained in:
@@ -2,13 +2,17 @@ package de.szimnau.zeitlaeufer;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static de.szimnau.zeitlaeufer.tools.CommonTools.print;
|
import static de.szimnau.zeitlaeufer.tools.CommonTools.print;
|
||||||
@@ -27,26 +31,41 @@ public class Main {
|
|||||||
|
|
||||||
private static void askParametersAndRun() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
private static void askParametersAndRun() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||||
Map<Integer, Class<?>> classes = getAllrelevantClasses();
|
SortedMap<Integer, Class<?>> classes = getAllrelevantClasses();
|
||||||
String printedClasses = classes.entrySet().stream()
|
String printedClasses = classes.entrySet().stream()
|
||||||
.map(e -> e.getKey() + ": " + e.getValue().getCanonicalName().substring(e.getValue().getCanonicalName().lastIndexOf('.') + 1))
|
.map(e -> e.getKey() + ": " + e.getValue().getCanonicalName().substring(e.getValue().getCanonicalName().lastIndexOf('.') + 1))
|
||||||
.reduce("", (a, b) -> a + "\n" + b);
|
.reduce("", (a, b) -> a + "\n" + b);
|
||||||
print("Welcher Zeitläufer soll verwendet werden? " + printedClasses + "\n: ");
|
print("Welcher Zeitläufer soll verwendet werden? " + printedClasses + "\n: ");
|
||||||
var num = Integer.parseInt(br.readLine());
|
var num = Integer.parseInt(br.readLine());
|
||||||
Class<?> selectedClass = classes.get(num);
|
Class<?> selectedClass = classes.get(num);
|
||||||
var mainMethod = selectedClass.getMethod("main", String[].class);
|
Method mainMethod = selectedClass.getMethod("main", String[].class);
|
||||||
mainMethod.invoke(null, (Object) new String[0]);
|
mainMethod.invoke(null, (Object) new String[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Map<Integer, Class<?>> getAllrelevantClasses() {
|
private static SortedMap<Integer, Class<?>> getAllrelevantClasses() {
|
||||||
InputStream stream = ClassLoader.getSystemClassLoader()
|
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||||
.getResourceAsStream("de/szimnau/zeitlaeufer");
|
List<String> fileNames = new ArrayList<>();
|
||||||
var br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
|
URL jarUrl = classLoader.getResource("de/szimnau/zeitlaeufer");
|
||||||
|
String path = jarUrl.getPath().split(":", 2)[1];
|
||||||
|
String cleanPath = path.substring(0, path.lastIndexOf('!'));
|
||||||
|
Enumeration<JarEntry> entries;
|
||||||
|
try (var jarFile = new JarFile(URLDecoder.decode(cleanPath, StandardCharsets.UTF_8))) {
|
||||||
|
entries = jarFile.entries();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
while (entries.hasMoreElements()) {
|
||||||
|
JarEntry entry = entries.nextElement();
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fileNames.add(entry.getName());
|
||||||
|
}
|
||||||
var increment = new AtomicInteger();
|
var increment = new AtomicInteger();
|
||||||
return Collections.unmodifiableSortedMap(new TreeMap<>(
|
return Collections.unmodifiableSortedMap(new TreeMap<>(
|
||||||
br.lines()
|
fileNames.stream()
|
||||||
.filter(line -> line.endsWith(".class") && !"Main.class".equals(line))
|
.filter(line -> line.endsWith(".class") && !line.endsWith("module-info.class") && !line.endsWith("Main.class"))
|
||||||
.sorted()
|
.sorted()
|
||||||
.map(Main::getClass)
|
.map(Main::getClass)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
@@ -61,23 +80,23 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
private static Class<?> getClass(String className) {
|
private static Class<?> getClass(String className) {
|
||||||
return getClassForName(className.substring(0, className.lastIndexOf('.')));
|
String classWithPath = className.replace("/", ".");
|
||||||
|
return getClassForName(classWithPath.substring(0, className.lastIndexOf('.')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Class<?> getClassForName(String className) {
|
private static Class<?> getClassForName(String className) {
|
||||||
String fqnClassName = "de.szimnau.zeitlaeufer." + className;
|
|
||||||
try {
|
try {
|
||||||
return Class.forName(fqnClassName);
|
return Class.forName(className);
|
||||||
} catch (ClassNotFoundException cnfe) {
|
} catch (ClassNotFoundException cnfe) {
|
||||||
println("ERROR: could not find Class for Name: de.szimnau.zeitlaeufer." + fqnClassName);
|
println("ERROR: could not find Class for Name: de.szimnau.zeitlaeufer." + className);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void parseParametersAndRun(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
private static void parseParametersAndRun(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
Class<?> selectedClass = getClassForName(args[0]);
|
Class<?> selectedClass = getClassForName("de.szimnau.zeitlaeufer." + args[0]);
|
||||||
if (selectedClass == null) {
|
if (selectedClass == null) {
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user