Compile the project using java and javac command line under ubuntu

1. java and javac command line

(1) javac command line

javac.exe is used to compile java source files and generate .class files

Syntax: javac [option] source

Commonly used [option] options:

A. -classpath <path> (-cp abbreviation): Specify the classpath to be used or the path of the jar package to be used (jar file, zip file (they are all wrong search files)), after use

       Will override the CLASSPATH setting

B. -d <path>: Specify the storage location of the .class file generated after the source file is compiled


(2) java command line

java.exe is used to run the .class file generated after javac is compiled

Syntax: java [option] classname [arguments]

Commonly used [option] options:

A. -classpath <path> (-cp abbreviation): Specify the classpath to be used or the path of the jar package to be used (jar file, zip file (they are all wrong search files)), after use

       Will override the CLASSPATH setting

B. [arguments]: Parameters passed to the main function


2. 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 file in current directory

java Demo ------------------- 执 艧 Demo.class


You can also specify the location where the .class file is generated through the -d option, such as:

javac -d .. Demo.java , the Demo.class file will be generated in the upper-level directory of the current directory, then when executing, the path of the class must be specified

java -cp .. Demo, which means to let java search for class files in the upper directory


(2) A java program with a package

Demo.java

package com.demo;
public class Demo{
    public static void main(String args[]){
               System.out.println("This is a test");
 }
}
Similarly, now compile the file:

javac Demo.java------------Generate Demo.class file in current directory

java Demo ------------------- 执 艧 Demo.class


You can also specify the location where the .class file is generated through the -d option, such as:

javac -d . Demo.java-----------Save the .class file according to the package path of com.demo

A com/demo/Demo.class file will be generated in the current directory, as shown below:

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

javac com / demo / Demo Someone 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 Tom();
        System.out.println("hello "+tom.getMyname());
}
}
Since the Friend class bar uses the Tom class, first compile Tom.java:javac -d . Tom.java, and then 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 import com.demo.Tom; this path is searched in the "." current directory indicated by -cp, and then generated according to the package structure com/demo/Friend.class, see the image below:

Run: java com.demo.Friend


(4) The class bar under one package uses the 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());
}
}
Still the same, now compile Tom.java, javac -d . Tom.java, and generate com/demo1/Tom.class;

Then compile Friend.java, javac -cp . -d . Friend.java, and generate com/demo2/Friend.class;


Run: java com.demo2.Friend;


(5) Java project closer to the project

Generally, there are three folders under a project: lib (imported jar package), classes (save .class file), and src (.java source code).

The directory structure of the java folder is as follows. The relationship between the files is that Client.java calls the methods of DemoService.java; ServerImpl.java calls the methods of DemoService.java; Server.java calls the methods of ServerImpl.java and DemoService.java ;

In the files below, I only show the packages and classes they reference.

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)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE 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 current position
TOP_DIR=$(pwd)

# Add all necessary jars
LIBPATH=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 file
javac -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/server
SIDE=$1

# Get current position
TOP_DIR=$(pwd)

# Add all necessary jars
LIBPATH=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 program
if [ $SIDE == "server" ];then
    java -cp $TOP_DIR/classes:$LIBPATH  com/server/Server
else
    java -cp $ TOP_DIR / classes: $ LIBPATH com / client / Client
be

Enter the thriftDemo folder and execute the ./make.sh script. The files generated in the classes folder are as follows:



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







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325916575&siteId=291194637