HelloWorld in Java can still play this way

Source: Internet
Author: User

You may see HelloWorld examples are not all think Bo Master brain is not a problem, how to take such a mentally retarded problems and examples to take. In fact, this is not the case, if so I believe I did not write this blog meaning. Because the first time I saw Java in the first example HelloWorld there are so many things. I was also listening to the confused force. Of course, this is the case, after the Meng forced to wake up, when I woke up the first reaction is to write a blog to commemorate the way I was crazy. Certainly hope next time everybody not to be confused, and very calmly say "HelloWorld always is HelloWorld always is so simple".

The development of Java This example, the use of development tools is the Notepad is enough, of course, if you use what eclipse, it is not interesting, and you use the eclipse is very confusing, Because you do not understand eclipse from the compilation of Java to run the entire mechanism of Java process, only know that the written, and then "Run as", that is to the code farm, I believe that everyone is to become siege lions and not yards farmers bar. The first step to becoming a siege lion is to learn this blog first, just for fun. Let's go

First, let's talk about the Java environment variable, open My Computer---> Properties---> Advanced system settings---> Environment variables

It is clear to everyone that the environment variables in Java are mainly configured with three operations: the first new java_home environment variable, the second configuration path, and the third configuration Classpath

Java_home= "The directory where the Bin folder is located in your JAVA jdk", appended to Path: "%java_home%\bin;%java_home%\jre", classpath= ".

"I believe you should be easy to understand why the previous two are so." I believe most people have a classpath= "." Why only one point can be configured. The meaning of this point is to indicate the meaning of the current directory. We all know that in Java, the. java files are actually compiled into a. class file that actually runs the. class file. Classpath= "." Indicates that the. class file will be found in the current directory path at the time of execution of the Run command; You may have seen this to configure Classpath. Classpath= ".;. /bin "; This means to find the corresponding specified. class file in the current directory, if not found, then go to the Bin directory in the current directory.

Why are you explaining this? This principle helps to understand the following we run Java programs.

Finish the configuration of the environment variables let's start writing our first example in Java

Before writing Java code, let's start by saying a top-level ternary in Java code: Package, Import, class

Maybe we don't pay much attention to these when we write the code, but these are the specs. So this HelloWorld example will follow this specification.

The first step: first create a demo folder on my desktop, and in the folder to create the Src and Bin folder, we all know that the SRC folder is the source code, bin mainly stored in the compiled. class file.

Step Two: Create a new text file under SRC, and modify the suffix named ". Java"

Step three: Turn on terminal compilation

First enter the SRC and bin directories instead of the SRC directory, run the Javac compile command:

You may see why it is different from our usual compile command, where we place the compiled bytecode file in the specified directory through the-D directory, meaning that the compiled bytecode file is placed under the Bin directory folder of the current directory. Then we can open the bin directory of the directory to see that the original is empty and now there is a package of helloworld.class file, because we write the code when we add the packages com.mikyou.test; So it's not surprising to see.

After the compilation is run, we all know that running we use the Java command on the line, is it so straightforward? We all know that we are not related to. java files in the process of running, we need to find a compiled. class file and then run it directly in Java.

First of all,-CP means:-CP path Specifies the class load path (CLASSPATH), why use-CP? This has to start with the configuration environment variable, that is, when we configure the environment variable, we configure a classpath as a point, And we say that this point means to look for the. class file in the current directory, and then our current directory is src and the directory where the bin is located does not have the. class file, so if the system environment variable to find, it must be found, so since the system can not be found, then we have to specify the class load by-CP The path. If you don't believe us, we can try.

The next step is to package our HelloWorld into a. jar file

We'll start with the SRC and bin directories and then into the bin, and then package all the files and directories in the bin directory into jars.

This jar command means to package all the files and directories in the Bin directory (* That means the table),-CVF (c is the compression command, v means that the process information that shows the compression is followed by the output file name) is actually similar to the Linux system compressed file command. After packing the file into the bin directory, the parent directory is the directory where the bin is located. After packing, you can see that there is a hello.jar in the same directory as Src,bin.

After packing, we will send this jar to the customer, how can the customer run it?

1. Put the. jar file into a disk and run it directly from the root directory of the disk: if I put it in the root of the C drive,

It's pretty useful to use the-CP.

Another way is to run the Hello.jar: put the. jar file into the extension class load library (the repository is in the Libs folder under the JRE directory in your JDK directory): mine is this E:\Android\Java\. Jdk8.0_11\jre\lib\ext: Run command: Java com.mikyou.test.HelloWorld

You will find that you can run it regardless of the path. And this method is a little bit better, you open Eclispe will find that the JRE in the load jar package has a hello.jar, that is, we can later encapsulate some of our own framework code into a jar package, and then placed in the directory, Each time you create a new program automatically load it is not necessary to create a new libs every time and then go to add Build path, it is very convenient.

Here we have a simple HelloWorld in Java is done, is not as simple as we imagined.

HelloWorld in Java can still play this way

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.