windows use simple command line javac, java, javap detailed presentation

Recently re-review a little java-based, we encountered some problems in the use of javap process, where they talk about how to file a class to compile, run, decompilation. Let yourself look deeper impression.

As stated, first of all we have on the desktop, Start -> Run -> type cmd Enter into the windows command line. Into the picture as shown:

image

It shows that the current default directory is C drive Users folder under the Administrator folder. In general, we are used to change the current directory. Because there are windows disk partition, to jump to another disk, such as E drive, there are several ways:

1, enter the command: the pushd path (this command to the current directory to a path of any desired existing)

2, enter the command: e:   transferred to the disk e, then enter cd known path to transfer desired.

Figure:

image

Want to use javac, java, javap commands in the windows command line, then the current computer must install the jdk and add jdk bin directory to environment variable path down. This goes without saying. So let's look at how to use javac, java, javap it.

A, javac

javac is used to compile .java files. Enter javac directly from the command line you can see a lot of usage tips, javac command prompt, I only know that common.

Javadi -d touch srcFile

Wherein: 1, -d destdir is used to specify the path where the compiler generated .class file. (;. "" If this option is omitted, the default generation .class files in the current directory, folder and the package is not generated by the current directory can be represented, namely: javac -d srcFile.)

       Note: In addition to adding the -d option to specify the compiler-generated .class file path, the biggest difference is the package name in the first line of the package keyword source file in the current folder path generation.

        2, srcFile source file path is .java files.

For example: There is such a simple java class path 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, then in the current path it swells into a JavacTest.class file, as shown:

image

Note: JavacTest.java is a first line marked package of java files, at which point it at a directory on your hard disk does not correspond to its package name, so, when run with the java com.stopTalking.test.JavacTest is unable to find the the java.

If the input, javac -d JavacTest.java, then the resulting JavacTest.class will be generated in the current directory files in the package, as shown:

image

 

Two, java

At this point, we want to run the class, most of the materials, the use of java JavacTest can be run directly, but we found this error:

image

This is because most of the materials used in class is the default package name, that is the first line of the source file is not specified package package name. The use of a class, we know, is required to use its fully qualified class name.

So, we enter on the command line: java com / stopTalking / test / JavacTest, you can see the correct result:

image

Summary: simply use the java command to run a .class file, not only need to use the fully qualified class name of the class, but also need to have this kind of package hierarchy folder in the current path. This must require the -d option when compiling. Otherwise you need to build their own package hierarchy folder.

Three, javap

javap primarily intended to help developers insights into the mechanisms of the Java compiler, the main options are:

-c decomposition method code, i.e. the specific method of displaying each bytecode

Private class member is used to specify what level displayed | -public | protected | package

-verbose specify further details display

Input javap -c com / stopTalking / test / JavacTest, FIG display:

image

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3586118.html

Guess you like

Origin blog.csdn.net/weixin_33877092/article/details/94354505