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

102
LINES

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

JavaX fragment (include)

1  
/*
2  
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat compile
3  
rm *.class
4  
javac -source 8 -target 8 Starter.java
5  
rm starter.jar
6  
zip starter.jar *.class
7  
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat upload
8  
j -noawt 1033518 starter.jar
9  
stefan@lenovo-ThinkPad-X220:~/dev/starter$ cat download
10  
wget -O Starter.java "https://code.botcompany.de:9898/getraw.php?id=1034391"
11  
*/
12  
13  
import java.io.File;
14  
import java.lang.reflect.Method;
15  
import java.net.*;
16  
import java.util.*;
17  
import java.util.Set;
18  
19  
public class Starter {
20  
    public static void main(String[] args) {
21  
        try {
22  
            new Starter(args);
23  
        } catch (Throwable e) {
24  
            e.printStackTrace();
25  
            System.exit(1);
26  
        }
27  
    }
28  
29  
    public static class StartClassLoader extends URLClassLoader {
30  
        Set<File> files = Collections.synchronizedSet(new LinkedHashSet());
31  
32  
        // keys: full class names or package names
33  
        // (inner classes are automatically included)
34  
        NavigableMap<String, ClassLoader> parentMap = syncTreeMap();
35  
        
36  
        Map<String, Class> loadedClasses = syncTreeMap();
37  
      
38  
        protected Class<?> findClass(String name) throws ClassNotFoundException {
39  
          ClassLoader parent = findParentForClass(name);
40  
          Class c = parent != null
41  
            ? parent.loadClass(name)
42  
            : super.findClass(name);
43  
          loadedClasses.put(name, c);
44  
          return c;
45  
        }
46  
          
47  
        ClassLoader findParentForClass(String name) {
48  
          ClassLoader cl = parentMap.get(name);
49  
          if (cl != null) return cl;
50  
          String s = parentMap.floorKey(name);
51  
          if (startsWithOneOf(name, s + "$", s + "."))
52  
            return parentMap.get(s);
53  
          return null;
54  
        }
55  
        
56  
        void addParent(String name, ClassLoader loader) {
57  
          parentMap.put(name, loader);
58  
        }
59  
    
60  
        public StartClassLoader() {
61  
            super(new URL[0], null);
62  
        }
63  
64  
        public StartClassLoader(ClassLoader parent) {
65  
            super(new URL[0], parent);
66  
        }
67  
68  
        public boolean addFile(File f) throws MalformedURLException {
69  
            if (!files.add(f))
70  
                return false;
71  
            addURL(f.toURI().toURL());
72  
            return true;
73  
        }
74  
    }
75  
76  
    Starter(String[] args) throws Exception {
77  
        Class c = getClass();
78  
        File jar = new File(c.getProtectionDomain().getCodeSource().getLocation().toURI());
79  
        System.out.println("Starter Jar: " + jar.getAbsolutePath());
80  
81  
        StartClassLoader startClassLoader = new StartClassLoader(/*c.getClassLoader()*/);
82  
        startClassLoader.addFile(jar);
83  
84  
        Class mainClass = startClassLoader.loadClass("main");
85  
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
86  
        mainMethod.setAccessible(true);
87  
        mainMethod.invoke(null, new Object[] {args});
88  
    }
89  
90  
    static NavigableMap syncTreeMap() {
91  
      return Collections.synchronizedNavigableMap(new TreeMap());
92  
    }
93  
94  
    static boolean startsWithOneOf(String s, String... l) {
95  
      for (String x : l) if (startsWith(s, x)) return true; return false;
96  
    }
97  
    
98  
    static boolean startsWith(String a, String b) {
99  
      return a != null && a.startsWith(b == null ? "" : b);
100  
    }
101  
102  
}

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: 168 / 227
Version history: 8 change(s)
Referenced in: [show references]