Execute Java and java files

Source: Internet
Author: User

Execute Java and java files


(6) LoadMainClass in Java. c

Location: jdk/src/share/bin/java. c

This method is used to load the class where the main function is located. This method first loads the sun. launcher. LauncherHelper class, and then calls the checkAndLoadMain class to find the class where the main function is located. The relevant code is as follows.

Jclass cls = GetLauncherHelperClass (env); // This function loads sun. launcher. launcherHelper class NULL_CHECK0 (cls); if (JLI_IsTraceLauncher () {start = CounterGet ();} NULL_CHECK0 (mid = (* env)-> GetStaticMethodID (env, cls, // locate the method id "checkAndLoadMain", "(ZILjava/lang/String;) Ljava/lang/Class;"); str = NewPlatformString (env, name ); result = (* env)-> CallStaticObjectMethod (env, cls, mid, USE_STDERR, mode, str); // call this static method


(7) checkAndLoadMain in LauncherHelper. java

Location: jdk/src/share/bin/classes/sun/launcher/LauncherHelper. java

Java code is more intuitive, so I wrote comments directly.

</Pre> <p> <pre name = "code" class = "java"> public static Class <?> CheckAndLoadMain (boolean printToStderr, int mode, String what) {initOutput (printToStderr); // mode determines whether to directly input the main class or jar file, that is, distinguish java-jar xxx. jar or java xxx. class String cn = null; switch (mode) {case LM_CLASS: cn = what; break; case LM_JAR: cn = getMainClassFromJar (what ); // The principle of this function is to extract the mainfest file in jar to obtain its main class break; default: // shocould never happen throw new InternalError ("" + mode + ": unknown launch mode ");} Cn = cn. replace ('/', '.'); Class <?> MainClass = null; try {mainClass = scloader. loadClass (cn); // if you use scloader to load data, it is not described here.} Catch (NoClassDefFoundError | ClassNotFoundException cnfe) {if (System. getProperty ("OS. name ",""). contains ("OS X") & Normalizer. isNormalized (cn, Normalizer. form. NFD )) {try {// On Mac OS X since all names with diacretic symbols are given as decomposed it // is possible that main class name comes incorrectly from the command line // and we have re-compose it mainClass = scloader. loadClass (N Ormalizer. normalize (cn, Normalizer. form. NFC);} catch (NoClassDefFoundError | ClassNotFoundException cnfe1) {abort (cnfe, "java. launcher. cls. error1 ", cn) ;}} else {abort (cnfe," java. launcher. cls. error1 ", cn) ;}// set to mainClass appClass = mainClass;/** Check if FXHelper can launch it using the FX launcher. in an FX app, * the main class may or may not have a main method, so do this befor E * validating the main class. */if (mainClass. equals (FXHelper. class) | FXHelper. doesExtendFXApplication (mainClass) {// Will abort () if there are problems with the FX runtime FXHelper. setFXLaunchParameters (what, mode); return FXHelper. class;} validateMainClass (mainClass); // the validity of the main class is verified here, and the code is attached to it. Return mainClass ;}


The code for verifying whether the main class is valid is provided. 

Static void validateMainClass (Class <?> MainClass) {Method mainMethod; try {mainMethod = mainClass. getMethod ("main", String []. class); // check whether the following signature method main (String [] argvs)} catch (NoSuchMethodException nsme) {// invalid main or not FX application, abort with an error abort (null, "java. launcher. cls. error4 ", mainClass. getName (), FXHelper. JAVAFX_APPLICATION_CLASS_NAME); return; // Avoid compiler issues} int mod = mainMethod. getModifiers (); // Get the qualifier if (! Modifier. isStatic (mod) {// if it is not static, the error "abort (null," java. launcher. cls. error2 "," static ", mainMethod. getDeclaringClass (). getName ();} if (mainMethod. getReturnType ()! = Java. lang. void. TYPE) {abort (null, "java. launcher. cls. error3 ", // if it is not void, the mainMethod is returned. getDeclaringClass (). getName ());}}


Ii. Summary


These two blogs mainly analyze what happens when java commands are executed. by reading the code, you can see how the configured environment variables work and when the jvm is initialized, and the process of loading and verifying the main class.


This blog has at least the following points that have not been thoroughly analyzed:

Jvm initialization.

The relationship between the env structure and the vm structure and its implementation.

How scloader works.

How to load LauncherHelper.

These problems may be analyzed in later blogs.



How are java files executed?

First, use the javac command to compile the. java file into a. class file (bytecode file)
Then, the JVM (java Virtual Machine) loads the. class file and translates it into a machine code to run the java program;
Learn Together ----- refer:
We manually execute the java program as follows:
1. Write the source program in notepad or UE text editor;
2. Use the javac command to compile the source program into a. class file;
The compiled. class (class bytecode) file contains the following content:

ConstantPool: symbol table;
FieldInfo: Information about member variables in the class;
MethodInfo: Method description in the class;
Attribute: optional additional nodes.
The FieldInfo node contains the names of member variables, such as public, private, and static. The ConstantValue attribute is used to store static unchanged member variable values. Deprecated and Synthetic are used to mark a member variable that is not recommended or generated by the compiler.

3. With the. class file, we can run the java program by executing the java interpretation command.

Now we will mainly discuss what will happen after the java command is executed?
First, the JVM mounts. class, that is, the class Loader loads class bytecode. A Class Loader is also a java class. Therefore, the Class Loader itself needs to be loaded by another class loader, which is similar to having an egg first or having a chicken first. However, the class loader in JAVA is easy to solve. JAVA Virtual Machine (JVM) is embedded with a Bootstrap class loader, which is implemented with local code specific to the operating system and belongs to the JAVA Virtual Machine kernel, bootstrap classes do not need dedicated class loaders for loading. The Bootstrap Class is responsible for loading the classes in the JAVA core package (that is, the classes in the rt. jar file). The returned value of the Class. getClassLoader () method of these classes is null, that is, the Bootstrap Class loader. The JAVA core package has two other class loaders: ExtClassLoader and AppClassLoader. They are all JAVA classes written in JAVA, the ExtClassLoader class is responsible for loading the classes in the jar package stored in the <JAVA_HOME>/jre/lib/ext directory, and the AppClassLoader is responsible for loading the startup execution class of the application, that is, when you use java commands to start and execute a class, the java virtual machine uses AppClassLoader to load the class. When compiling and running JAVA programs, you can use the ExtClassLoader class loader to search for the classes to be loaded in the JAR package under the <JAVA_HOME>/jre/lib/ext directory, if you want to include a jar package such as Servlet API or javamail. copy the jar package to this directory. when compiling the Servlet or JavaMail program, you do not need to add the jar package containing the Servlet API or javamail to the classpath environment variable. jar package file.
The above is a general process of JAVA program execution.

Java cannot run

Open the Java Control Panel ---> Security
Click to edit the Site List
Paste your website
OK

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.