Compare commits
9 Commits
31d8cfd43d
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc690f3350 | ||
|
|
8be9cf81d7 | ||
|
|
7df22a2d1f | ||
|
|
664c6108a5 | ||
|
|
094dfd9daf | ||
|
|
4dd18d03f0 | ||
|
|
5a596e28da | ||
|
|
3929ce2666 | ||
|
|
c872efa459 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
target/
|
||||
dist/
|
||||
/target/
|
||||
/dist/
|
||||
*.class
|
||||
/.idea/
|
||||
/*.iml
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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";
|
||||
mkdir $tmpDir;
|
||||
cp dist/zeitlaeufer.jar $tmpDir;
|
||||
cd $originDir;
|
||||
# java -cp $tmpDir/zeitlaeufer.jar $@;
|
||||
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
|
||||
|
||||
133
src/main/java/de/szimnau/zeitlaeufer/Main.java
Normal file
133
src/main/java/de/szimnau/zeitlaeufer/Main.java
Normal 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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user