Background description

In order to better learn the JVM read “Java Virtual Machine specification”, “write your own Java Virtual machine”, especially “automatic manual write Java virtual machine” can see the whole picture more clearly. It’s best for developers to learn something hands-on, make some hands-on demos, and feel secure when the results are delivered.

Case description

This chapter is mainly through the preparation of Java code, from the main method entry to obtain instructions. For example; -version

Environment to prepare

  1. The JDK 1.8.0 comes with
  2. IntelliJ IDEA Community Edition 2018.3.1×64

Configuration information

  1. Debug configuration
    1. Configuration position: Run/Debug Configurations -> Program Arguments
    2. Configuration content: -version

Code sample

itstack-demo-jvm-01├ ─ ─ pom. XML └ ─ ─ the SRC └ ─ ─ the main │ └ ─ ─ Java │ └ ─ ─ org. Itstack. Demo. The JVM │ ├ ─ ─ Cmd. Java │ └ ─ ─ main. Java └ ─ ─ the test └ ─ ─ Java └ ─ ─ Org. Itstack. Demo. Test └ ─ ─ the HelloWorld. JavaCopy the code

pom.xml

<! -- command line argument parser -->
<dependency>
  <groupId>com.beust</groupId>
  <artifactId>jcommander</artifactId>
  <version>1.72</version>
</dependency>
Copy the code

Cmd.java

package org.itstack.demo.jvm;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import java.util.List;

/** * http://www.itstack.org * create by fuzhengwei on 2019/4/24 */
public class Cmd {

    @Parameter(names = {"-?"."-help"}, description = "print help message", order = 3, help = true)
    boolean helpFlag = false;

    @Parameter(names = "-version", description = "print version and exit", order = 2)
    boolean versionFlag = false;

    @Parameter(names = {"-cp"."-classpath"}, description = "classpath", order = 1)
    String classpath;

    @Parameter(description = "main class and args")
    List<String> mainClassAndArgs;

    boolean ok;

    String getMainClass(a) {
        returnmainClassAndArgs ! =null && !mainClassAndArgs.isEmpty()
                ? mainClassAndArgs.get(0)
                : null;
    }

    List<String> getAppArgs(a) {
        returnmainClassAndArgs ! =null && mainClassAndArgs.size() > 1
                ? mainClassAndArgs.subList(1, mainClassAndArgs.size())
                : null;
    }

    static Cmd parse(String[] argv) {
        Cmd args = new Cmd();
        JCommander cmd = JCommander.newBuilder().addObject(args).build();
        cmd.parse(argv);
        args.ok = true;
        returnargs; }}Copy the code

Main.java

package org.itstack.demo.jvm;

/** * http://www.itstack.org * create by fuzhengwei on 2019/4/24 * program arguments: -version */
public class Main {

    public static void main(String[] args) {
        Cmd cmd = Cmd.parse(args);
        if(! cmd.ok || cmd.helpFlag) { System.out.println("Usage: 
      
[-options] class [args...] "
); return; } if (cmd.versionFlag) { System.out.println("Java version \" 1.8.0 comes with \ ""); return; } startJVM(cmd); } private static void startJVM(Cmd cmd) { System.out.printf("classpath:%s class:%s args:%s\n", cmd.classpath, cmd.getMainClass(), cmd.getAppArgs()); }}Copy the code

The test results

java version "1.8.0 comes with"
Copy the code

Wechat search “bugStack wormhole stack” public number, attention after reply “with Java to achieve JVM source code” to obtain the source of this article & more original special case!