How to run the entire Java project with Javac and Java compilation

Source: Internet
Author: User
Tags echo command

Transferred from: http://blog.csdn.net/huagong_adu/article/details/6929817

This article teaches you how to use Javac and Java commands, as well as how to use scripts (shell or BAT) to facilitate processing, and to demonstrate these usages with a simple example.

The IDE is a double-edged sword, it can do anything to help you, you just hit a few lines of code, a few mouse clicks, the program ran up, it is quite convenient to use. You don't have to worry about what's behind it, what commands are executed, and what the principles are based. However, this excessive reliance often loses the most basic skills, and when it comes to a place where there is no IDE, you don't know what to do, and you don't have a code to run. Like give you a bottle of water, you do not know how to open to drink, and then died of thirst.
        The commands used to compile and run the Myeclipse,java file are basically forgotten. Now the project out of the prototype, put to the server to test, SSH landing on the server on the dumbfounded, are command line, the previous program icon has become a cloud, the program put on the Do not know how to compile and run, only to fill up the missed lessons, below to do the next lesson notes.
First, Javac command
Javac is used to compile Java files in the following format:
        java [options] [SourceFiles] [@files]
        which
        Options: command line option;
        SourceFiles: One or more source files to be compiled;
        @files: One or more files to list the source file, sometimes to compile a lot of files, a dozen commands will appear very long, also inconvenient to modify, you can compile the source files listed in the file, before the file name plus @, so that you can compile a number of files, to compile a project is very useful, convenient, easy.
        There are several more important options:
        -D is used to specify the location of the compiled class file, which by default does not specify a directory for the class file, and the compiled class file will be in the same directory as the source file;
        The-classpath can be abbreviated to-CP, which is used to search for the class file required for compiling, to indicate the location of the class file used by the compilation, such as a jar, zip, or other directory containing the class file, specifying that the option overrides the Classpath setting;
        -sourcepath is used to search for the source files (i.e. Java files) required for compilation, specifying the location of the source files to be searched, such as jars, zip, or other directories containing Java files;
        Note the difference between the file path separators under Windows and Linux and the file list (that is,-classpath and-sourcepath specified files) separators:
        Windows under File path delimiter with \, file list delimiter with semicolon;
        Linux under File path delimiter with/, file list delimiter with colon:
Second, Java command
Java is used to execute programs in the following format:
        java [options] Classfile
        Options: command-line options, typically used to-classpath specify the location of the file to be executed and the classpath to be used, including the jar, zip, and class file directories, overwriting classpath settings
Third, the script
If you want to knock the command is very long, every time to compile and run the time to re-knock, this is a very painful thing, so the script can greatly facilitate your workload. Under Linux with shell scripts, Windows uses the Bat batch program. Because it is under Linux, I'm just a brief introduction to the shell, about the Bat batch program syntax itself Baidu, not difficult.
1. Opening
        Linux has many different shells, usually using bash (Bourne again Shell), and the program must start with the following line:
        #!/bin/sh
        #! is used to tell the system to use the following parameters to execute the program, the/bin/sh is used here
        To enable your script to execute, you must also have the executable permission for the file, and use the following command to change the file permissions:
        chmod +x filename
2. Notes
        Start with a sentence in # to show the comment, until the end of the line, write more comments to help later look at the time to know what they are doing
3. Variables
        Shell script variables are strings, not the declaration type, when defined by the direct variable = value can be used in the variable when using the $ variable or ${variable},echo command for printing, for example:

[Python]View Plaincopy
    1. #!/bin/sh
    2. # define variable words, value Hello World
    3. words="Hello World"
    4. # Print the value of the variable words
    5. Echo $words

4. Command
        The shell script can be used to directly use the Linux command, you need to use any command to directly knock in. Keep in mind some common commands:
        CD Open Directory
        Ls-l Displaying directory information
        RM-FR recursively delete directories and the following files without prompting for information
        mkdir Creating a Directory
        PWD Displays the current path
        Kill-9 PID forces the process of killing a process number
        Pkill the process of killing a name
        PS aux display running process information
        Netstat-pan Viewing network Port monitoring
Iv. Examples

Here is the file compile, which compiles the entire Java project and places the compiled file in the specified directory:

[Python]View Plaincopy
  1. #!/bin/sh
  2. # Define Some constants
  3. Onsserver=onsserver
  4. Project_path=/root/iot-oid
  5. Jar_path= $PROJECT _path/lib
  6. Bin_path= $PROJECT _path/bin
  7. Src_path= $PROJECT _path/src/$ONSSERVER
  8. # First remove the Sources.list file if it exists and then create the sources file of the project
  9. Rm-f $SRC _path/sources
  10. Find $SRC _path/com-name *.java > $SRC _path/sources.list
  11. # First remove the Onsserver directory if it exists and then create the bin directory of Onsserver
  12. RM-RF $BIN _path/$ONSSERVER
  13. mkdir $BIN _path/$ONSSERVER
  14. # Compile The Project
  15. javac-d $BIN _path/$ONSSERVER-classpath $JAR _path/jdom.jar: $JAR _path/oro-2.0. 8.jar @ $SRC _path/sources.list

Here is the file run, which executes the program:

[Python]View Plaincopy
    1. #!/bin/sh  
    2.    
    3. # DEFINE SOME CONSTANTS  
    4. onsserver=onsserver  
    5. PROJECT_PATH=/ROOT/IOT-OID  
    6. Jar_path= $PROJECT _path/lib  
    7. bin_path= $PROJECT _path/bin  
    8.   
    9. # run the project as a  background process  
    10. nohup 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, with a relatively simple, if there are errors, please correct me!
Personal original, brain products, is not easy, welcome reprint, reproduced please indicate the source!

Reference documents:
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

How to run the entire Java project with Javac and Java compilation

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.