The Windows command line Java and javac javap use the detailed of Java compilation command

  • 2020-04-01 03:12:44
  • OfStack

As the question goes, first we are on the desktop, start -> Run - > Type CMD enter to enter the Windows command line. Enter the screen as shown in the picture:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139002.png" >

It is known that the current default directory is the Administrator folder under the Users folder of C disk. In general, we are used to changing the current directory. Since Windows has disk partitions, there are several ways to jump to another disk, such as disk E:

1. Enter command: pushd path (this command can set the current directory to any existing path as desired)

2. Enter the command: e:  Move to disk e, then enter CD and move to the desired known path.

As shown in figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139003.png" >

To use javac, Java, javap, and so on from the Windows command line, you must have JDK installed on your current computer and add the JDK bin directory to your environment variable path. That goes without saying. So let's take a look at how to use javac, Java, and javap.

A, javac

Javac is used to compile.java files. When I type javac directly from the command line, I see a lot of hints about how to use the javac command.

Javac -d destdir srcFile

Where: 1, -d destdir is used to specify the path to store the compiled. Class files. If this option is omitted, the.class file is generated by default in the current directory and no package folder is generated; The current directory can be represented by ". ", i.e. Javac-d. srcFile)

Note: in addition to specifying the path to the compiled.class file, the biggest difference is that you can generate folders under the package name under the package keyword on the first line of the source file under the current path.

2. SrcFile is the path of the source file. Java file.

For example: there is such a simple Java class, the path is E:\test\ javactest.java:


package com.stopTalking.test;      public class JavacTest {          public static void main(String[] args) {       
        byte a = 5;        
        short b = 6;        
        System.out.println("JavacTest [a=" + a + ", b=" + b +  "]");        
    }        
}


Under the current path, enter javac javactest.java, and a javactest.class file will be generated under the current path, as shown in the following figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139004.png" >

Note: JavacTest. Java is a first line marking the package of Java files, while it on a hard disk directory is not corresponding to its package name, so, use Java com. StopTalking. Test. JavacTest is unable to find the Java runtime.

If you type in the javac copy.javactest.java, the generated javactest.class will be in the package file generated in the current directory, as shown in the figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139005.png" >

Second, the Java

At this point, we want to run the class, which in most of the textbooks would run directly using Java JavacTest, but we find this error:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139006.png" >

This is because most textbooks use classes with default package names, meaning that the first line of the source file does not have the package name specified. To use a class, we know that we need to use its fully qualified class name.

So, we in the command line, enter: Java com/stopTalking/test/JavacTest, can see the correct results:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139007.png" >

Summary: to run a.class file with a simple Java command, you not only need to use the fully qualified class name of the class, but you also need a package-level folder with the class under the current path. This must require compile-time use of the -d option. Otherwise you'll need to create your own package-level folders.

Third, javap

Javap is designed to help developers gain insight into the mechanics of the Java compiler. The main options are:

-c decomposes the method code, that is, displays the bytecode specific to each method

-public | protected | package | private specifies which level of class member to display

-verbose specifies that further details are displayed

Input javap -c com/stopTalking/test/JavacTest, display as shown in figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201403/201403231139008.png" >


Related articles: