Before discussing running an application with java commands Let's take a look at what Java applications are.
The following is a rough classification of Java applications. The explanation is also rough, so if you are interested, please check it yourself.
--Console app Apps that can be used interactively from the console, such as Prompt and Terminal. --Desktop app An app that is complete within one device. Sometimes referred to as a GUI. --Web app An application that processes by communication between a web server and a client. --Android app Desktop app for Android. (It's an analogy) --Embedded programs Programs built directly into hardware, such as home appliances.
The java command mainly executes console apps and desktop apps.
Executing the java command launches a Java application. The java tool first starts the JRE and loads the class specified by the command. Called inside the main method by calling the main method of the loaded class The application will be executed while loading other classes in sequence.
To specify a class that has a main method, You can specify the class directly or you can specify the JAR file.
The main method is public and static, it takes a String type array as an argument, Must be a method that satisfies that it does not return a return value.
main method
public static void main(String[] args) {
//Some processing
}
# java [ options ] class [ argument ... ]
$ java HelloWorld "args1" "args2"
In the above example, the main method of the HelloWorld class is called, and the argument args of the main method is An array of type String containing "args1" and "args2" is passed.
You must specify the fully specified class name to execute the class file. Since the above example did not have a package configuration, It could be executed without specifying a package, but if you have a package configuration You must specify the class name in a fully specified package.
#The current directory is/src
# /With src as the package root, Hello World/src/com/example/If it is directly under the app
$ java com.example.app.HelloWorld
If you specify a JAR file, from the class file archived in the JAR file In order to identify the startup class that has the main method, put the Main-Class header in the manifest file. Must be set. The following article was easy to understand for details around here.
Reference article: Introduction to JAR files @ hiwm0126
To start the JAR file, specify the location of the JAR file. (Both absolute path and relative path are possible) Also, if you specify a JAR file for the startup class, you need to add the -jar option.
#The current directory is/(root)
# /lib/jars/sample-app.Run the jar
$ java -jar /lib/jars/sample-app.jar
--- classpath (or -cp)
$ java -Dsun.boot.class.path="/sample/path" SampleApp
SampleApp class
//Set value "/sample/"path" can be obtained
System.getProperty("sun.boot.class.path");
This option enables / disables Java assertions. Assertions are disabled by default.
--- enableassertions (or -ea):
Enabled with the default package (anonymous package) in the current directory.
-
HotSpot is a general term for technologies for speeding up processing built into the JVM. A type of JIT compiler (just-in-time compiler) that only uses methods that have been executed a certain number of times or more. A mechanism for processing optimization such as compiling is prepared.
HotSpot includes Java HotSpot Client VM (client version) and Java HotSpot Server VM (server version) There are two types, each of which has the following characteristics.
--Client version Focus on compiling targets for important classes and methods, and emphasize high-speed loading. --Server version It compiles as a whole, so loading is slow, but optimization is important.
The following are options for specifying the HotSpot to use.
Java has a mechanism to use programs coded other than Java. With agents (JVMTI clients) via the Java Virtual Machine Tool Interface (JVMTI) It can be used by exchanging. This agent is a variety of tools implemented in languages other than Java.
The following options are options for using these native agent libraries.
--- agentlib:
Now let's run the sample code.
In the sample code, if Null or an empty string is passed, "NG" will be displayed. In other cases, 3 processing results of the method that returns the argument string are output. You should see three lines, "NG", "NG", and "Hello World!".
#The current directory is/java-sample/src
$ java -cp /java-sample/lib/commons-lang3-3.10.jar:/java-sample/src com.example.app.UseCommons
#Below is the output result
NG
NG
Hello World!