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

87
LINES

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

JavaX fragment (include)

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();
      
        protected Class<?> findClass(String name) throws ClassNotFoundException {
          ClassLoader parent = findParentForClass(name);
          if (parent != null)
            return parent.loadClass(name);
          else
            return super.findClass(name);
        }
          
        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/7
Text MD5: 92ad89c7054044a37e49578a6d7d3a5c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-19 19:20:50
Source code size: 2861 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 43 / 75
Version history: 6 change(s)
Referenced in: [show references]