Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyApp {
- private static MyApp instance = null;
- private static boolean started = false;
- public MyApp() {
- if (instance != null) {
- throw new IllegalStateException("MyApp already initialized");
- }
- instance = this;
- }
- public static MyApp getInstance() {
- return instance;
- }
- public void startup() {
- synchronized (LOCK) {
- if (started == true) {
- throw new IllegalStateException("MyApp is already running");
- }
- }
- // do things to start the GUI, etc...
- // ... ... ...
- started = true;
- }
- }
- public class Launcher {
- public static void main(String[] args) {
- new Launcher().start();
- }
- private void start() {
- // create custom classloader ...
- // ... ... ...
- Class<?> myAppClass = myLoader.loadClass("com.something.MyApp");
- // calls the MyApp constructor and sets the "instance" static var to "this"
- Object instance = myAppClass.newInstance();
- Method startupMethod = myAppClass.getMethod("startup");
- // this seems to call the MyApp constructor again!, exception thrown...
- startupMethod.invoke(instance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement