Printing message on Console without using main() method

后端 未结 10 1768
情歌与酒
情歌与酒 2020-11-27 03:04

I was asked this question in an interview.

How to print message on console without using main() method?

10条回答
  •  一向
    一向 (楼主)
    2020-11-27 03:24

    You could define a custom class loader that prints your message :

    public class MyClassLoader extends ClassLoader {
        public MyClassLoader(ClassLoader other) {
             super(other);
             System.out.println("Hi there");
             System.exit(0);
        }
    }
    

    Then run the java command :

    java -Djava.system.class.loader=MyClassLoader

    (don't need to add a class as parameter)

提交回复
热议问题