jvm如何进入public static void main?

问题描述:

jvm如何进入默认类:

How can jvm enter in default class:

class try1
{
public static void main(String args[])
{
    ...
}
}

其中jvm如何访问此方法?

In it how does jvm access this method?

在包中,如果某个类是默认的,则其公共方法无法从外部访问,那么jvm如何进入这个类?

In packages if a class is default its public methods cant be accessed from outside, so how does jvm enter this class?

调用 main $的不是JVM本身c $ c>方法。这是Java启动器的工作,即 java.exe

Java启动程序是一个用C编写的小程序,它使用常规JNI函数

It is not JVM itself who invokes main method. This is rather a job of Java launcher, i.e. java.exe.
Java launcher is a small program written in C that uses regular JNI functions:


  1. JNI_CreateJavaVM 创建JVM的新实例并获取 JNIEnv的实例 ;

  2. JNIEnv :: FindClass 找到命令行中指定的主类;

  3. JNIEnv :: GetStaticMethodID 在类#2中找到 public static void main(String [])方法。

  4. JNIEnv :: CallStaticVoidMethod 调用#3中的方法。

  1. JNI_CreateJavaVM to create a new instance of JVM and to obtain an instance of JNIEnv;
  2. JNIEnv::FindClass to locate the main class specified in the command line;
  3. JNIEnv::GetStaticMethodID to find public static void main(String[]) method in class #2.
  4. JNIEnv::CallStaticVoidMethod to invoke the method found in #3.

事实上,JNI允许您使用所有类,方法和字段,即使使用 private 修饰符。

In fact, JNI allows you to work with all classes, methods and fields, even with private modifier.