What happens when java program starts?

不想你离开。 提交于 2019-12-02 21:12:55

•Who and how decides which classes should be loaded at startup and which once needed?

we need to understand the fundamentals of java class loading. Initially bootstrap classloader (it is implemented natively as part of the VM itself) is responsible for loading core system classes. Then there are other class loaders as well like Extension, system, user-defined(optional) class loaders which decide when and how classes should be loaded. Fundamentals of class loading

The decision is made by the classloader. There are different implementations, some of which pre-load all classes they can and some only loading classes as they are needed.

A class only needs to be loaded when it is accessed from the program code for the first time; this access may be the instantiation of an object from that class or access to one of its static members. Usually, the default classloader will lazily load classes when they are needed.

Some classes cannot be relied on to be pre-loaded in any case however: Classes accessed via Class.forName(...) may not be determined until this code is actually exectued.

Among other options, for simple experiments, you can use static initializer code to have a look at the actual time and order in which classes are actually loaded; this code will be executed when the class is loaded for the first time; example:

class SomeClass {

    static {
        System.out.println("Class SomeClass was initialized.");
    }

    public SomeClass() {
        ...
    }

    ...

}

Your example shows an executable jar, which is simply a normal java archive (jar) with an extra key/value pair in it's manifest file (located in folder "META_INF"). The key is "Main-Class" and the value the fully qualified classname of that class whose "main" method will be executed, if you "run" the jar just like in your example.

A jar is a zip file and you can have a look inside with every zip archive tool.

Karthi Teja

Whenever you compile a Java program the following steps takes place

  1. First the Class Loader loads the class into the JVM.
  2. After giving the command javac filename.java the compiler checks for compile time errors and if everything is fine then it will generate the .Class files(byte code).

This will be the first phase.

Later the interpreter checks for the runtime errors and if everything is fine without exceptions then the interpreter converts the byte code to executable code.

First phase in java is done by the JIT compiler(Just In Time).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!