From dc690f3350858256850b384453a95d8a3cc397bf Mon Sep 17 00:00:00 2001 From: fszimnau Date: Wed, 3 Dec 2025 15:13:16 +0100 Subject: [PATCH] - enable running project 'quck' - run the jar in the dist directory directly - enable running any class directly without needing a jar in the first place --- runProject.sh | 28 ++++++--- .../java/de/szimnau/zeitlaeufer/Main.java | 58 +++++++++++++------ 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/runProject.sh b/runProject.sh index 91cf044..0e100ff 100644 --- a/runProject.sh +++ b/runProject.sh @@ -2,13 +2,25 @@ set -uo pipefail # when executed as executable file in "git for windows" bash some things won't work, so always run with prefixed command -originDir=$(pwd); -cd /g/zeitlaeufer/; -tmpDir="/tmp/zeitlaeufer_$RANDOM"; -mkdir $tmpDir; -cp dist/zeitlaeufer.jar $tmpDir; -cd $originDir; +jarDir=/g/zeitlaeufer/dist/; +if [[ $# == 0 || ($1 != -quick && $1 != -class) ]]; then + tmpDir=/tmp/zeitlaeufer_$RANDOM; + mkdir $tmpDir; + cp ${jarDir}zeitlaeufer.jar $tmpDir; + jarDir=$tmpDir; +elif [[ $1 == -quick ]]; then + shift; +elif [[ $1 == -class ]]; then + originDir=$(pwd); + cd /g/zeitlaeufer/target/; + java de.szimnau.zeitlaeufer.$2; + exitCode=$?; + cd originDir; + return $exitCode; +fi # java -jar $tmpDir/zeitlaeufer.jar $@; # -p <=> --module-path | -m <=> --module -java --module-path $tmpDir/zeitlaeufer.jar --module zeitlaeufer $@; -rm -r $tmpDir; +java --module-path $jarDir/zeitlaeufer.jar --module zeitlaeufer $@; +if [[ -n $tmpDir && -d $tmpDir ]]; then + rm -r $tmpDir; +fi diff --git a/src/main/java/de/szimnau/zeitlaeufer/Main.java b/src/main/java/de/szimnau/zeitlaeufer/Main.java index dd3baf6..26adb76 100644 --- a/src/main/java/de/szimnau/zeitlaeufer/Main.java +++ b/src/main/java/de/szimnau/zeitlaeufer/Main.java @@ -2,6 +2,7 @@ package de.szimnau.zeitlaeufer; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -45,25 +46,15 @@ public class Main { private static SortedMap> getAllrelevantClasses() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - List fileNames = new ArrayList<>(); - URL jarUrl = classLoader.getResource("de/szimnau/zeitlaeufer"); - if (jarUrl == null) { + URL resourceUrl = classLoader.getResource("de/szimnau/zeitlaeufer"); + if (resourceUrl == null) { throw new RuntimeException("Kann ausführbare Klassen nicht eruieren, da keine Ressource \"de/szimnau/zeitlaeufer\" verfügbar."); } - String path = jarUrl.getPath().split(":", 2)[1]; - String cleanPath = path.substring(0, path.lastIndexOf('!')); - Enumeration entries; - try (var jarFile = new JarFile(URLDecoder.decode(cleanPath, StandardCharsets.UTF_8))) { - entries = jarFile.entries(); - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - if (entry.isDirectory()) { - continue; - } - fileNames.add(entry.getName()); - } - } catch (IOException e) { - throw new RuntimeException("Kann JAR-Datei zwecks Reflection nicht öffnen:", e); + Set fileNames; + if (resourceUrl.getPath().contains(".jar")) { + fileNames = getFileNamesStreamFromJar(resourceUrl); + } else { + fileNames = getFileNamesStreamFromPackage(resourceUrl); } var increment = new AtomicInteger(); return Collections.unmodifiableSortedMap(new TreeMap<>( @@ -82,6 +73,39 @@ public class Main { } + private static Set getFileNamesStreamFromJar(URL resourceUrl) { + Set fileNames = new HashSet<>(); + String path = resourceUrl.getPath().split(":", 2)[1]; + String cleanPath = path.substring(0, path.lastIndexOf('!')); + Enumeration entries; + try (var jarFile = new JarFile(URLDecoder.decode(cleanPath, StandardCharsets.UTF_8))) { + entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (entry.isDirectory()) { + continue; + } + fileNames.add(entry.getName()); + } + } catch (IOException e) { + throw new RuntimeException("Kann JAR-Datei zwecks Reflection nicht öffnen:", e); + } + return fileNames; + } + + + private static Set getFileNamesStreamFromPackage(URL resourceUrl) { + try (InputStream stream = resourceUrl.openStream(); + var br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { + return br.lines() + .map(line -> "de/szimnau/zeitlaeufer." + line) + .collect(Collectors.toSet()); + } catch (IOException e) { + throw new RuntimeException("ausführbare Klassen nicht eruieren, da Ressource " + resourceUrl + " nicht auslesbar.", e); + } + } + + private static Class getClass(String className) { String classWithPath = className.replace("/", "."); return getClassForName(classWithPath.substring(0, className.lastIndexOf('.')));