Compare commits

...

9 Commits

Author SHA1 Message Date
fszimnau
dc690f3350 - 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
2025-12-03 15:13:16 +01:00
fszimnau
8be9cf81d7 work with jar file before closing it 2025-11-25 09:21:53 +01:00
fszimnau
7df22a2d1f improved/ added error messages 2025-11-20 09:07:29 +01:00
fszimnau
664c6108a5 Learn to work when run via jar instead of via .class file directly 2025-11-19 13:57:28 +01:00
fszimnau
094dfd9daf corrected alternative execute command 2025-11-19 11:12:01 +01:00
fszimnau
4dd18d03f0 added explanatory comment 2025-11-19 11:03:28 +01:00
fszimnau
5a596e28da set main class as entry point 2025-11-19 10:49:01 +01:00
fszimnau
3929ce2666 added "main" class as main entrypoint 2025-11-19 10:45:14 +01:00
fszimnau
c872efa459 adjusted gitignore (to intellij usage) 2025-11-19 10:42:28 +01:00
4 changed files with 164 additions and 12 deletions

6
.gitignore vendored
View File

@@ -1,3 +1,5 @@
target/
dist/
/target/
/dist/
*.class
/.idea/
/*.iml

View File

@@ -6,4 +6,9 @@ if [[ ! -e dist ]]; then
mkdir dist;
fi
rm -r dist/* 2>/dev/null;
jar cf ../dist/zeitlaeufer.jar target/**/*;
dir=$(pwd)
cd target;
# jar [...] --manifest=[...] with main class does work when running with "java -jar" but not with "java --module-path"
# --create --file <=> cf
jar --create --file=../dist/zeitlaeufer.jar --main-class=de.szimnau.zeitlaeufer.Main ./**/*;
cd $dir

View File

@@ -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 ~/zeitlaeufer/;
tmpDir="/tmp/zeitlaeufer_$RANDOM";
jarDir=/g/zeitlaeufer/dist/;
if [[ $# == 0 || ($1 != -quick && $1 != -class) ]]; then
tmpDir=/tmp/zeitlaeufer_$RANDOM;
mkdir $tmpDir;
cp dist/zeitlaeufer.jar $tmpDir;
cd $originDir;
# java -cp $tmpDir/zeitlaeufer.jar $@;
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/$@;
java --module-path $jarDir/zeitlaeufer.jar --module zeitlaeufer $@;
if [[ -n $tmpDir && -d $tmpDir ]]; then
rm -r $tmpDir;
fi

View File

@@ -0,0 +1,133 @@
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;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import static de.szimnau.zeitlaeufer.tools.CommonTools.print;
import static de.szimnau.zeitlaeufer.tools.CommonTools.println;
public class Main {
public static void main(String[] args) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
if (args.length == 0) {
askParametersAndRun();
} else {
parseParametersAndRun(args);
}
}
private static void askParametersAndRun() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
SortedMap<Integer, Class<?>> classes = getAllrelevantClasses();
String printedClasses = classes.entrySet().stream()
.map(e -> e.getKey() + ": " + e.getValue().getCanonicalName().substring(e.getValue().getCanonicalName().lastIndexOf('.') + 1))
.reduce("", (a, b) -> a + "\n" + b);
print("Welcher Zeitläufer soll verwendet werden? " + printedClasses + "\n: ");
var num = Integer.parseInt(br.readLine());
Class<?> selectedClass = classes.get(num);
Method mainMethod = selectedClass.getMethod("main", String[].class);
mainMethod.invoke(null, (Object) new String[0]);
}
private static SortedMap<Integer, Class<?>> getAllrelevantClasses() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
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.");
}
Set<String> fileNames;
if (resourceUrl.getPath().contains(".jar")) {
fileNames = getFileNamesStreamFromJar(resourceUrl);
} else {
fileNames = getFileNamesStreamFromPackage(resourceUrl);
}
var increment = new AtomicInteger();
return Collections.unmodifiableSortedMap(new TreeMap<>(
fileNames.stream()
.filter(line -> line.endsWith(".class") && !line.endsWith("module-info.class") && !line.endsWith("Main.class"))
.sorted()
.map(Main::getClass)
.filter(Objects::nonNull)
.filter(clazz ->
Arrays.stream(clazz.getMethods()).anyMatch(m ->
"main".equals(m.getName()) && m.getParameterCount() == 1 && m.getParameterTypes()[0] == String[].class
)
)
.collect(Collectors.toMap(c -> increment.incrementAndGet(), Function.identity()))
));
}
private static Set<String> getFileNamesStreamFromJar(URL resourceUrl) {
Set<String> fileNames = new HashSet<>();
String path = resourceUrl.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();
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<String> 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('.')));
}
private static Class<?> getClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException cnfe) {
println("ERROR: could not find Class for Name: de.szimnau.zeitlaeufer." + className);
}
return null;
}
private static void parseParametersAndRun(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> selectedClass = getClassForName("de.szimnau.zeitlaeufer." + args[0]);
if (selectedClass == null) {
System.exit(1);
}
var mainMethod = selectedClass.getMethod("main", String[].class);
mainMethod.invoke(null, (Object) Arrays.copyOfRange(args, 1, args.length));
}
}