Introduction to java command

Chapter 4 Execute Code

Java application type

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.

Java command overview

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.

main method conventions

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
    }

Basic configuration of java command

# 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.

Executing class files

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

Executing the JAR file

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

java command options

Major options

--- classpath (or -cp) Specify the search destination of the user class. When multiple selections are made, separate them with a semicolon (Windows) or a colon (Unix). --- jar Specify the JAR file in the startup class. Specify the location of the JAR file in the path.

$ java -Dsun.boot.class.path="/sample/path" SampleApp

SampleApp class


//Set value "/sample/"path" can be obtained
System.getProperty("sun.boot.class.path");

Assertion options

This option enables / disables Java assertions. Assertions are disabled by default.

--- enableassertions (or -ea): Enable assertions. There are the following types of application range specifications. - ... Enabled for the specified package and its subpackages. - ...
Enabled with the default package (anonymous package) in the current directory. - Enabled in the specified class. --No arguments Valid for all classes except system classes. ---disableassertions (or -da): Disable assertions. Specifying the scope is the same as the -enableassertions option. --- enablesystemassertions (or -esa) Enable assertions for all system classes. ---disablesystemassertions (or -dsa) Disable assertions for all system classes.

Debug options

HotSpot options

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.

Native agent library options

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: = Specify and load the native agent library. --- agentpath: = Specify and load the native agent library with the full path.

Run

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!

Return to main page

Recommended Posts

Introduction to java command
Introduction to java
Introduction to javadoc command
Introduction to jar command
Introduction to javac command
[Java] Introduction to lambda expressions
[Java] Introduction to Stream API
[Introduction to rock-paper-scissors games] Java
[Introduction to Java] About lambda expressions
[Introduction to Java] About Stream API
Introduction to Functional Programming (Java, Javascript)
Introduction to Ruby 2
Introduction to SWING
Introduction to Micronaut 1 ~ Introduction ~
Introduction to migration
Introduction to Doma
Introduction to java for the first time # 2
[Introduction to Java] How to write a Java program
Output of the book "Introduction to Java"
Introduction to monitoring from Java Touching Prometheus
Introduction to Java Web Apps Performance Troubleshooting
Introduction to algorithms with java --Search (breadth-first search)
Java Performance Chapter 1 Introduction
Introduction to JAR files
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Introduction to Ratpack (8)-Session
[Introduction to Java] About type conversion (cast, promotion)
Introduction to algorithms with java --Search (bit full search)
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
[Java] Connect to MySQL
[Monthly 2017/04] Introduction to groovy !! ~ Java grammar / specification comparison ~
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Kotlin's improvements to Java
From Java to Ruby !!
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
An introduction to Groovy for tedious Java engineers
[Introduction to Java] Basics of java arithmetic (for beginners)
Java Lambda Command Pattern
Introduction to Java for beginners Basic knowledge of Java language ①
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
Introduction to Design Patterns (Builder)
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
Convert Java Powerpoint to XPS
Java adds table to PDF
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications