Java Note: Part 4: Java program first run; Part 4: java
# Daily nagging # This blog won't be published on time, because Xiaoxing has something to do today. However, I was eager to hear from fans, so I stayed up late last night. Thanks to xiaoxinghahahao (for example, I have to finish my Java notes like watching TV series )!
Next, you need to know that the HelloWorld we just compiled is not really a nominal Java code. Why? Because the file type of the code we write is a text document, not a Java file, how can we change the file type?
First, we need to display the file extension name (not required for expansion), open the file resource manager (the simplest way to open it: Double-click my computer/this computer), and select the file bar:
(Do not care about other things)
Then, select change folder and search options:
Select the view function menu:
Find the extension name that hides the known file type from the drop-down list and remove the preceding check box:
(Remove, not check)
Then click OK:
(If you are not sure, it will not be saved, and you will get it blank)
Close the file resource manager, find our previous HelloWorld code, right-click and rename:
Replace the. txt suffix with the. java Suffix:
Click the desktop and submit the rename button. The following page is displayed:
(For children who have downloaded the editor, your document should change the icon, for example :)
Then, follow the method in the previous blog to open DOS (cmd) and enter the file path ...... Ah, just forget that you don't know the basic doscommand. Right-click the HelloWorld code and select properties:
Copy/copy the location information (Ctrl + C ):
Close the window and enter cd + paste in cmd. (do not paste the two words. I want you to paste the copied content, Ctrl + V.) (Note, there is a space between cd and pasted content ):
(If it is placed in a disk, such as an edisk, enter the disk identifier, for example:
)
Enter the javac + file name. suffix (similarly, do not type the file name. suffix, and there is a space between javac and file name. suffix ):
In this case, we can stop it and look back at the directory where the HelloWorld file was put. Is there an additional HelloWorld. class file? I will write about the cause. First, you will have an impression:
Enter the java + class name (similarly, do not name the class, and there is a space behind java. Class I haven't written yet. You can understand it as a file name without a suffix ):
As you can see, the doscommand bar is changed to the Java Console (of course you can't see it, what will be written in the console later), and then output a sentence: Hello World! There is also a \ r \ n (carriage return line feed), the detailed principle will be written later.
After the Java code is run, if a problem occurs, if the 'java' written in the previous article is not an internal or external command, it is not a runable program or a batch processing file. Check the Java environment variables. If there are other errors, you can reply in the following comments or send me a private message. I will reply after seeing it. When the program runs successfully, think about how Java code runs?
Java is a compilation-type programming language (the difference between compilation and interpretation is Baidu. In fact, Java is a semi-compilation and semi-interpretation, but we should ignore it first ), therefore, all programs must be compiled. Convert our Java file into a bytecode Class file. In this step, Java provides a program: javac. We use javac to compile our HelloWorld. java program as the HelloWorld. class bytecode file (so a class file is added above ). Then we passed the java command to run the program, but to be precise, it was run on JVM. What is JVM? I wrote in my previous blog that is a Java virtual machine. Therefore, all Java programs run on the Java Virtual Machine.
The following figure shows the Java mechanism:
In addition, the benefit of compiling and running the program is that we can run the program everywhere once we compile it. We have compiled and run the HelloWorld program before, but we can still run it again on the Java Virtual Machine:
How are you doing!