The use of IDE tools in Java development will certainly greatly improve development efficiency, but sometimes it is necessary to use Java commands to start Java Engineering Code on the server to complete a series of functions, of course, Ant is also very convenient. Here's a simple HelloWorld code to record how to use the most basic Java commands.
First, write HelloWorld, the code is as follows:
Copy Code code as follows:
public class hello{
public static void Main (string[] args) {
System.out.println ("Hello World");
}
}
Second, use the Javac command to compile.
Open the cmd window
Enter the directory where the file is located and the Hello.java directory above is F:\JavaProject\ahellodemo\src\.
Compile using the Javac Hello.java. When you are done, you can see a hello.class file under F:\JavaProject\ahellodemo\src\.
Use the Java command to run the compiled code.
Run the compiled code in the CMD window using the Java Hello Command, outputting Hello world.
The above process is an introduction to Java practice. So what if this class is under a package? Suppose the structure of the engineering directory in which the class is located is as follows
--ahellodemo
--src
--com
--whty
--hello
--hello.java
--classes
1). The Helloworld.java code is as follows:
Copy Code code as follows:
Package Com.whty.hello;
public class hello{
public static void Main (string[] args) {
System.out.println ("Hello World");
}
}
2. At this point, we want the code to be compiled into the classes directory (the directory needs to be built itself) just as it would with the IDE. The following is still compiled using the Javac command.
Open the cmd window
Access to the project directory, the above engineering directory is F:\JavaProject\ahellodemo\.
compile with javac-d./classes./src/com/whty/hello/hello.java. When you are done, you can see a COM directory under F:\JavaProject\ahellodemo\classes\, and Hello.class files are compiled to the F:\JavaProject\ahellodemo\classes\com\ The Whty\hello directory.
3. Run the compiled code with the Java command.
In the CMD window into the engineering directory under the classes directory, and then use the Java Com.whty.hello.Hello command to run the compiled code, output Hello world.
4). Make a running Jar pack
After the above three steps, we can first write a MANIFEST.MF file in the classes directory below the contents of the following, we should pay attention to each colon after the English space:
Copy Code code as follows:
manifest-version:1.0
Main-class:com.whty.hello.hello
Class-path:
Then cmd into the project classes directory, using the JAR-CVFM hello.jar manifest.mf com command, then the next occurrence of the directory Hello.jar file. Entering Java-jar Hello.jar in the cmd window will output Hello world.
See more Java syntax, you can focus on: "Thinking in the Java Chinese manual", "JDK 1.7 reference manual in English," "JDK 1.6 API Java Chinese Reference manual", "JDK 1.5 API Java Chinese Reference manual", also hope that the big A lot of home support cloud habitat community.