Using IDE tools in Java development will definitely improve development efficiency to a great extent, but sometimes you need to use java commands to start Java engineering code on the server to complete a series of functions. Of course, using ANT is also very convenient. The following is a simple HelloWorld code as an example to record how to use the most basic Java commands.
1. Write HelloWorld, the code is as follows:
Copy the code as follows: public class Hello{
public static void main(String[] args) {
System.out.println("Hello world");
}
}
2. Use javac commands to compile.
Open the CMD window and enter the directory where the file is located. The directory where Hello.java is located is F:/JavaProject/ahellodemo/src/.
Compile using javac Hello.java. After completion, you can see that there is an additional Hello.class file under F:/JavaProject/ahellodemo/src/
3. Use java commands to run the compiled code.
Run the compiled code using the java Hello command in the CMD window and output Hello world.
The above process is a practical introduction to Java. So what if this class is under a package? Assume that the project directory structure of this class is as follows
--ahellodemo
--src
--com
--whty
--hello
--Hello.java
--classes
1).HelloWorld.java code is as follows:
The code copy is as follows: package com.whty.hello;
public class Hello{
public static void main(String[] args) {
System.out.println("Hello world");
}
}
2). At this time, we hope that the code can be compiled into the classes directory (this directory needs to be created by itself) just like using the IDE. The following is still compiled using the javac command.
Open the CMD window and enter the directory where the project is located. The above project directory is F:/JavaProject/ahellodemo/.
Compile with javac -d ./classes ./src/com/whty/hello/Hello.java. After completion, you can see that there is an additional com directory under F:/JavaProject/ahellodemo/classes/, and the Hello.class file has also been compiled into the F:/JavaProject/ahellodemo/classes/com/whty/hello directory.
3). Use java command to run the compiled code.
Enter the classes directory in the project directory in the CMD window, and then use the java com.whty.hello.Hello command to run the compiled code and output Hello world.
4). Make it into a runnable jar package
After the above three steps, we can first write a MANIFEST.mf file in the classes directory as follows. Pay attention to the English spaces after each colon:
Copy the code as follows: Manifest-Version: 1.0
Main-Class: com.whty.hello.Hello
Class-Path:
Then enter the project classes directory in CMD and use the jar -cvfm hello.jar MANIFEST.mf com command. At this time, the hello.jar file appears in the directory. Entering java -jar hello.jar in the CMD window will output Hello world.
To view more Java syntax, you can follow: "Thinking in Java Chinese Manual", "JDK 1.7 Reference Manual Official English Version", "JDK 1.6 API java Chinese Reference Manual", "JDK 1.5 API java Chinese Reference Manual". I also hope that everyone will support Wulin.com more.