I recently reviewed the java basics and encountered some problems when using javap. Here I will talk about how to compile, run, and decompile a class file. It also gives you a better impression.
For example, first on the desktop, choose Start> RUN> press cmd to enter the windows command line. Enter the screen:
The default directory is the Administrator folder under the Users folder of drive C. In general, we are used to changing the current directory. Because windows has disk partitions, you can skip to another disk, for example, an edisk:
1. Enter the pushd path (this command can set the current directory to any existing path you want)
2. Enter the command: e: transfer to the edisk, and then enter the cd to the desired known path.
If you want to use commands such as javac, java, and javap in the windows command line, you must install jdk on your computer and add the bin directory of jdk to the environment variable path. Needless to say. Let's take a look at how to use javac, java, and javap.
I. javac
Javac is used to compile. java files. You can directly enter javac in the command line to view a large number of prompts, prompting you to use the javac command. I only know what is commonly used.
Javac-d destdir srcFile
Where: 1.-d destdir is used to specify the path for storing the. class file generated by compilation. (If this option is omitted, it is generated in the current directory by default. class file, and no package folder is generated; the current directory can use ". javac-d. srcFile)
Note: In addition to specifying the path of the. class file generated by compilation, add the-d option. The biggest difference is that you can generate a folder under the package keyword of the source file in the current path.
2. srcFile is the path of the source file. java file.
For example, there is a simple java class with the 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 + "]");
}
}
Enter javac JavacTest. java in the current path to generate a JavacTest. class file,
Note: JavacTest. java is a java file marked with a package in the first line. At this time, its directory on the hard disk does not correspond to its package name. Therefore, java com. stopTalking. test. java cannot be found during JavacTest running.
If javac-d. JavacTest. java is entered, the generated JavacTest. class is in the package file generated in the current directory,
Ii. java
In this case, we want to run this class. In most teaching materials, we can use java JavacTest to directly run the class. However, we found this error:
This is because most of the textbooks use the default package name, that is, the first line of the source file does not specify the package name. When using a class, we know that we need to use its fully qualified class name.
Therefore, enter java com/stopTalking/test/JavacTest in the command line to view the correct result:
Summary: To run a. class file using a simple java command, you not only need to use the fully qualified class name of the class, but also need to have the package level folder of the class under the current path. This requires the use of the-d option during compilation. Otherwise, you must create a package level folder.