Use Java, Javac command line to compile the project under Ubuntu

Source: Internet
Author: User
Tags log4j

One, Java and Javac command line
(1) Javac command line

Javac.exe for compiling Java source files, generating. class files

Syntax: javac [option] Source

Common [option] options:

A,-classpath < path > (-CP abbreviation): Specifies the classpath to use or the path of the jar package to be used (the jar file, the zip file (which is the wrong search file)), after use

Will overwrite the classpath settings

B,-D < path >: Specify the location where the. class file is generated after the source file is compiled


(2) Java command line

Java.exe used to run the. class file generated after Javac compilation

Syntax: Java [option] classname [arguments]

Common [option] options:

A,-classpath < path > (-CP abbreviation): Specifies the classpath to use or the path of the jar package to be used (the jar file, the zip file (which is the wrong search file)), after use

Will overwrite the classpath settings

B, [arguments]: arguments passed to the main function


Ii. examples
(1) A single Java program

Demo.java

public class demo{public    static void Main (String args[]) {               System.out.println ("This is a Test");}}

Javac demo.java-----------Generate demo.class files in the current directory

Java Demo-------------------Execution Demo.class


You can also specify where to build the. class file by using the-D option, such as:

Javac-d. Demo.java, the Demo.class file will be generated at the top level of the current directory, then the path of class must be specified when executing

JAVA-CP. Demo, indicating that Java searches the class file at the top level of the directory


(2) A Java program with packages

Demo.java

Package Com.demo;public class demo{public    static void Main (String args[]) {               System.out.println ("This is a test") ; }}
Similarly, the file is now compiled:

Javac demo.java-----------Generate demo.class files in the current directory

Java Demo-------------------Execution Demo.class


You can also specify where to build the. class file by using the-D option, such as:

Javac-d. Demo.java-----------Save the. class file according to the Com.demo package path

A com/demo/demo.class file is generated in the current directory, such as:

Then, when executing, specify the path to the class file:

Javac Com/demo/demo or Javac Com.demo.Demo


(3) Under the same package, one class calls another class

Tom.java

Package Com.demo;public class tom{public String getmyname () {return ' this is tom! ';}}

Friend.java

Package Com.demo;import Com.demo.tom;public class friend{public    static void Main (String args[]) {        tom tom = new to M ();        System.out.println ("Hello" +tom.getmyname ());}}
Because the friend class uses the Tom class, first must compile tom.java:javac-d. Tom.java, the Com/demo/tom.class file will be generated according to the package structure.

Next compile the Friend.java file: JAVAC-CP.  -D. Friend.java,javac will be based on, import com.demo.Tom; this path to-CP indicates "." Search under current directory, and then generate Com/demo/friend.class according to package structure, see:

Running: Java com.demo.Friend


(4) class bar under one package with class under another package


Tom.java

Package Com.demo1;public class tom{public String getmyname () {return ' this is tom! ';}}

Friend.java

Package Com.demo2;import Com.demo1.tom;public class friend{public    static void Main (String args[]) {        tom tom = new Tom ();        System.out.println ("Hello" +tom.getmyname ());}}
is still the same, now compiled tom.java,javac-d. Tom.java, generate Com/demo1/tom.class;

Then compile the FRIEND.JAVA,JAVAC-CP.   -D. Friend.java, generate Com/demo2/friend.class;


Run: Java com.demo2.Friend;


(5) Closer to project Java engineering

Generally, a project below will have Lib (the necessary jar package introduced), classes (save. class file), SRC (. Java source code) three folders.

The directory structure of the Java folder is as follows The relationship between the files is a method that Client.java calls the Demoservice.java; Serverimpl.java calls the Demoservice.java method; Server.java calls Serverimpl.java and Demoservice . Java methods;

The following files, I only show the packages and classes they refer to.

Src/com/client/client.java:

Package Com.client;import Org.apache.thrift.texception;import Org.apache.thrift.protocol.tbinaryprotocol;import Org.apache.thrift.protocol.tprotocol;import Org.apache.thrift.transport.tsocket;import Org.apache.thrift.transport.ttransport;import Com.demo.DemoService; public class Client {..... ..... ..................... ........ ................... .......}
Src/com/demo/demoservice.java:
/** * autogenerated by Thrift Compiler (0.9.0) * * does not EDIT unless is sure you KNOW what is DOING * @generated */package com.demo;import org.apache.thrift.scheme.ischeme;import org.apache.thrift . Scheme. Schemefactory;import Org.apache.thrift.scheme.standardscheme;import Org.apache.thrift.scheme.tuplescheme;import Org.apache.thrift.protocol.ttupleprotocol;import Org.apache.thrift.protocol.tprotocolexception;import Org.apache.thrift.encodingutils;import Org.apache.thrift.texception;import Java.util.list;import Java.util.arraylist;import java.util.map;import java.util.hashmap;import Java.util.enummap;import java.util.Set; Import Java.util.hashset;import java.util.enumset;import Java.util.collections;import Java.util.BitSet;import Java.nio.bytebuffer;import Java.util.arrays;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class Demoservice {...} ...}, ..... ...} 
...}. ...........................}........}.
Src/com/server/server.java:

Package Com.server;import Org.apache.thrift.tprocessor;import Org.apache.thrift.protocol.tbinaryprotocol;import Org.apache.thrift.server.tserver;import Org.apache.thrift.server.tsimpleserver;import Org.apache.thrift.transport.tserversocket;import Org.apache.thrift.transport.ttransportexception;import Com.demo.demoservice;import Com.server.serverimpl;public Class Server { ..................................................................................}
The make.sh script is as follows:

#!/bin/bash# Get positiontop_dir=$ (pwd) # ADD all necessary jarslibpath=lib/commons-codec-1.6.jar:lib/ commons-logging-1.1.1.jar:lib/httpclient-4.2.5.jar:lib/httpcore-4.2.4.jar:lib/junit-4.4.jar:lib/ libthrift-1.0.0.jar:lib/log4j-1.2.14.jar:lib/servlet-api-2.5.jar:lib/slf4j-api-1.5.8.jar:lib/ Slf4j-log4j12-1.5.8.jar#compile java FILEJAVAC-CP $LIBPATH                  Src/com/demo/demoservice.java-  D./classes/. JAVAC-CP $TOP _dir/classes: $LIBPATH src/com/server/serverimpl.java-d/classes/.javac-cp $TOP _dir/classes: $LIBPATH Src/com/server/server.java     -D./CLASSES/.JAVAC-CP $TOP _dir/classes: $LIBPATH Src/com/client/client.java-     D ./classes/.

The run.sh script is as follows:

#!/bin/bash#get client/serverside=$1# Get Current positiontop_dir=$ (PWD) # ADD all necessary jarslibpath=lib/ commons-codec-1.6.jar:lib/commons-logging-1.1.1.jar:lib/httpclient-4.2.5.jar:lib/httpcore-4.2.4.jar:lib/ Junit-4.4.jar:lib/libthrift-1.0.0.jar:lib/log4j-1.2.14.jar:lib/servlet-api-2.5.jar:lib/slf4j-api-1.5.8.jar:lib /slf4j-log4j12-1.5.8.jar#run programif [$SIDE = = "Server"];then    java-cp $TOP _dir/classes: $LIBPATH  com/ Server/serverelse    java-cp $TOP _dir/classes: $LIBPATH  Com/client/clientfi
Go to the Thriftdemo folder, execute the./make.sh script, and the files generated under the Classes folder are as follows:



Reference: wenku.baidu.com/view/f4c19dbc65ce0508763213c6.html







Use Java, Javac command line to compile the project under Ubuntu

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.