jvm如何进入public static void main? [英] how does jvm enter in public static void main?

查看:112
本文介绍了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 方法。这是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.

这篇关于jvm如何进入public static void main?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆