Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

102
LINES

< > BotCompany Repo | #1034391 // Starter.java

JavaX fragment (include)

/*
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat compile
rm *.class
javac -source 8 -target 8 Starter.java
rm starter.jar
zip starter.jar *.class
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat upload
j -noawt 1033518 starter.jar
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat download
wget -O Starter.java "https://code.botcompany.de:9898/getraw.php?id=1034391"
*/

import java.io.File;
import java.lang.reflect.Method;
import java.net.*;
import java.util.*;
import java.util.Set;

public class Starter {
    public static void main(String[] args) {
        try {
            new Starter(args);
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static class StartClassLoader extends URLClassLoader {
        Set<File> files = Collections.synchronizedSet(new LinkedHashSet());

        // keys: full class names or package names
        // (inner classes are automatically included)
        NavigableMap<String, ClassLoader> parentMap = syncTreeMap();
        
        Map<String, Class> loadedClasses = syncTreeMap();
      
        protected Class<?> findClass(String name) throws ClassNotFoundException {
          ClassLoader parent = findParentForClass(name);
          Class c = parent != null
            ? parent.loadClass(name)
            : super.findClass(name);
          loadedClasses.put(name, c);
          return c;
        }
          
        ClassLoader findParentForClass(String name) {
          ClassLoader cl = parentMap.get(name);
          if (cl != null) return cl;
          String s = parentMap.floorKey(name);
          if (startsWithOneOf(name, s + "$", s + "."))
            return parentMap.get(s);
          return null;
        }
        
        void addParent(String name, ClassLoader loader) {
          parentMap.put(name, loader);
        }
    
        public StartClassLoader() {
            super(new URL[0], null);
        }

        public StartClassLoader(ClassLoader parent) {
            super(new URL[0], parent);
        }

        public boolean addFile(File f) throws MalformedURLException {
            if (!files.add(f))
                return false;
            addURL(f.toURI().toURL());
            return true;
        }
    }

    Starter(String[] args) throws Exception {
        Class c = getClass();
        File jar = new File(c.getProtectionDomain().getCodeSource().getLocation().toURI());
        System.out.println("Starter Jar: " + jar.getAbsolutePath());

        StartClassLoader startClassLoader = new StartClassLoader(/*c.getClassLoader()*/);
        startClassLoader.addFile(jar);

        Class mainClass = startClassLoader.loadClass("main");
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        mainMethod.setAccessible(true);
        mainMethod.invoke(null, new Object[] {args});
    }

    static NavigableMap syncTreeMap() {
      return Collections.synchronizedNavigableMap(new TreeMap());
    }

    static boolean startsWithOneOf(String s, String... l) {
      for (String x : l) if (startsWith(s, x)) return true; return false;
    }
    
    static boolean startsWith(String a, String b) {
      return a != null && a.startsWith(b == null ? "" : b);
    }

}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

-
Snippet ID: #1034391
Snippet name: Starter.java
Eternal ID of this version: #1034391/9
Text MD5: 42a7b984fa5943cd56ee1f5f2e62a70b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-15 16:38:15
Source code size: 3347 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 169 / 227
Version history: 8 change(s)
Referenced in: