SoFunction
Updated on 2025-03-02

How to use java compilation commands and startup commands

Compile commands

In Java development, compiling Java source files (usually with .java as the extension) is an indispensable step.

This step is done through the javac command, which is a command-line tool for the Java Compiler.

The compiled code generates bytecode files that are named .class as the extension and can be run on a Java virtual machine (JVM).

Basic syntax

javac [options] sourcefiles
  • options: Options available at compile time to adjust the compiler's behavior.
  • sourcefiles: One or more Java source files to be compiled.

Common options

-d destinationdir

This option specifies the directory where the compiled .class file should be stored.

If you do not use this option, the .class file will be generated by default in the same directory as the source file.

javac -d out src/com/example/

The above command will place the compiled .class file in the out/com/example/ directory.

-classpath or -cp

Specifies the search path used to find user class files and annotation processors.

This option is particularly important when compiling Java source files that depend on other classes.

javac -cp lib/ src/com/example/

-sourcepath

Specifies the search path used to find the input source file. If not set, the user directory is used by default.

-encoding

Sets the character encoding used by the source file.

javac -encoding UTF-8 src/com/example/

-verbose

Output detailed compilation information, including each step performed by the compiler.

-X

Enable non-standard options, which may vary depending on the compiler implementation.

Start command

In Java development, starting Java applications usually uses java commands.

This command is used to start a Java virtual machine (JVM) and load the specified class (usually a class containing the main method) to run the Java program.

The following is a detailed explanation of java commands and an introduction to some common options.

Basic syntax

java [options] class [args...]
or
java [options] -jar jarfile [args...]
  • options: Optional parameters of Java commands to control the behavior of the JVM.
  • class: The name of the class to run containing the main method (excluding the .class suffix).
  • args...: Command line arguments passed to the main method.
  • -jar: Specifies that the next parameter is a JAR file that contains a file that specifies the entry point (that is, the class containing the main method).
  • jarfile: The name of the JAR file to be run.

Common options

-classpath or -cp

Specifies the path to search class files (.class) and packages (directory and ZIP/JAR files).

java -cp .:lib/* 

On Windows, the path separator is;, and on UNIX/Linux/macOS is:

-D

Set system properties.

java -DpropertyName=propertyValue 

-jar

Run the JAR file.

java -jar 

-javaagent

Enable Java Agent to load the agent when the JVM is started.

java -javaagent: -jar 

-Xmx and -Xms

Sets the maximum and initial heap memory size.

java -Xmx1024m -Xms512m 

-Xdebug and -Xrunjdwp

Options for debugging that enable remote debugging of Java applications.

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar 

-version

Print Java version information.

java -version

-verbose

Provides detailed information about class loading.

java -verbose 

-X

Non-standard options for debugging and non-production environments. These options may vary by JVM implementation.

Comprehensive examples

Suppose you have a Java source file with the following content:

public class HelloWorld {  
    public static void main(String[] args) {  
        ("Hello, World!");  
    }  
}

You can compile it with the following command:

javac 

If the compilation is successful, a bytecode file named   will be generated.

To output a .class file to a specific directory (for example, bin), you can use the -d option:

javac -d bin 

This creates a directory tree corresponding to the package structure in the bin directory and places the .class file inside it.

run

java -jar 

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.