How to compile and run the entire Java project with javac and Java

Source: Internet
Author: User
Tags echo command

Preface:This article explains how to use javac and Java commands, and how to use scripts (shell or BAT) for ease of processing, and demonstrate these usage with simple instances.

IDE is a double-edged sword. It can help you with everything. You just need to knock a few lines of code and click a few mouse clicks to run the program, which is quite convenient to use. You don't have to worry about what is done next to it, what commands are executed, and what principles are based on. However, this type of excessive dependency often leads to the loss of the most basic skills. When there is no ide, you feel that you have no idea how to run the code. It's like giving you a bottle of water. You don't know how to open it and drink it, and then you can live and die of thirst.
I used to use myeclipse before. The commands for compiling and running Java files were basically forgotten. Now that the project has a prototype and put it on the server for testing, SSH is dumpfounded as soon as it is logged on to the land server. It is a command line, and the program icons and everything has become a float cloud, I don't know how to compile and run the program. I can only make up the course. Next I will take the makeup notes.
I. javac commands
Javac is used to compile Java files in the following format:
Java [Options] [sourcefiles] [@ files]
Where:
Options: command line options;
Sourcefiles: one or more source files to be compiled;
@ Files: one or more files that list the source files. Sometimes there are many files to be compiled, and it may be very long or inconvenient to modify one by one, you can put the source file to be compiled in the file and add @ before the file name, so that you can compile multiple files, which is useful for compiling a project, convenient and easy-to-use.
There are several important options:
-D is used to specify the storage location of the compiled class file. By default, the storage directory of the class file is not specified. The compiled class file will be in the same directory as the source file;
-Classpath can be abbreviated as-CP, which is used to search for the class files required for compilation and point out the location of the class files used for compilation, such as jar, zip, or other directories containing class files, specifying this option will overwrite the classpath setting;
-Sourcepath is used to search for the source files (java files) required for compilation. It specifies the location of the source files to be searched, such as jar, zip, or other directories containing java files;
Note the differences between the file path separator and the file list Separator in Windows and Linux (that is, the file specified by-classpath and-sourcepath:
In Windows, the file path separator is \, and the file list separator is a semicolon;
In Linux, the file path separator is/, and the file list separator is colon:
Ii. Java commands
Java is used to execute the program. The format is as follows:
Java [Options] classfile
Options: Specifies the location of the file to be executed and the class path to be used, including the jar, zip, and class file directories. This option overwrites the classpath setting.
Iii. Script
If the command to be typed is long and you need to repeat it every time you compile and run it, this is a very painful thing, so using scripts can greatly facilitate your workload. Use shell scripts in Linux and bat batch processing programs in windows. Because it is in Linux, I will just give a brief introduction to shell here. It is not difficult to explain Baidu's syntax about the bat batch processing program.
1. Start
Linux has many different shells, usually using bash (Bourne again shell). The program must start with the following line:
#! /Bin/sh
#! It is used to tell the system to use the following parameters to execute the program. Here/bin/sh is used.
To execute your script, you must grant the executable permission to the file. Use the following command to change the File Permission:
Chmod + x filename
2. Notes
Comments are represented in the sentence starting with # until the end of this line. Writing more comments will help you know what you are doing later.
3. Variables
The variables in shell scripts are strings. You do not need to declare the type. when defining the variable, you can directly use the variable = value. When using the variable, you can use the $ variable or $ {Variable} and the echo command for printing, for example:

#! /Bin/sh # define the variable words with the value Hello World words = "Hello World" # print the value of the variable words echo $ words

4. Commands
In shell scripts, you can directly use Linux commands. You can simply enter the commands you need. Remember some common commands:
CD Open Directory
Ls-l displays directory information
Rm-fr recursively deletes directories and the files below. No message is displayed.
Mkdir create directory
PWD displays the current path
Kill-9 PID forces the process of killing a process number
Pkill: Kill a process with a name
PS aux displays running process information
Netstat-Pan: View network port monitoring
Iv. Example

The following is the file compile, which is used to compile the entire Java project and put the compiled file under the specified directory:

#!/bin/sh# Define some constantsONSSERVER=ONSServerPROJECT_PATH=/root/iot-oidJAR_PATH=$PROJECT_PATH/libBIN_PATH=$PROJECT_PATH/binSRC_PATH=$PROJECT_PATH/src/$ONSSERVER# First remove the sources.list file if it exists and then create the sources file of the projectrm -f $SRC_PATH/sourcesfind $SRC_PATH/com -name *.java > $SRC_PATH/sources.list# First remove the ONSServer directory if it exists and then create the bin directory of ONSServerrm -rf $BIN_PATH/$ONSSERVERmkdir $BIN_PATH/$ONSSERVER# Compile the projectjavac -d $BIN_PATH/$ONSSERVER -classpath $JAR_PATH/jdom.jar:$JAR_PATH/oro-2.0.8.jar @$SRC_PATH/sources.list

The following is the file run used to execute the program:

#!/bin/sh# Define some constantsONSSERVER=ONSServerPROJECT_PATH=/root/iot-oidJAR_PATH=$PROJECT_PATH/libBIN_PATH=$PROJECT_PATH/bin# Run the project as a background processnohup java -classpath $BIN_PATH:$JAR_PATH/jdom.jar:$JAR_PATH/oro-2.0.8.jar com.ONSServer.DoUDPRequest &

The above is a simple summary of javac, Java, and shell scripts. It is easy to use. If you have any errors, please correct me!
Personal originality and mental products are not easy. You are welcome to repost them. Please indicate the source for reprinting!

References:
Javac and Java commands:
Http://jeffchen.iteye.com/blog/395671
Http://www.blogjava.net/pdw2009/archive/2008/06/12/207413.html? Opt = Admin
Shell programming:
Http://bbs.chinaunix.net/thread-391751-1-1.html
Http://hi.baidu.com/zccamy/blog/item/b5220f94517de10e7bf48057.html

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.