Details of the class file method for running Java

  • 2020-04-01 03:42:53
  • OfStack

Run the class file

Execute the class file with the main method, command behavior:
Java < CLASS file name >
Note: the CLASS file name does not have the file suffix. CLASS

Such as:


java Test

If the class file is executed with a package, then: package < The package name >

That should be executed in the base path of the package, command behavior:
Java < The package name > .class file name
For example, in packagetest.java, the package name is: com.ee2ee.test, and the corresponding statement is:
Package com. Ee2ee. Test;
Packagetest. Java and the compiled class file packagetest. class are stored in the following directory:
classes
  | __com
          | __ee2ee
                    | __test
                              | __PackageTest. Java
                              | __PackageTest. Class
To run PackageTest. Class, execute it in the classes directory:


java com.ee2ee.test.PackageTest

Run the class in the jar file

              The principle is the same as running the class file, just add the parameter -cp < Jar file name > Can.
              For example: the execution of the test. The jar the classes in com. Ee2ee. Test. PackageTest, command line is as follows:


java -cp test.jar com.ee2ee.test.PackageTest

Display JDK version information

              When there are more than one JDK version on a machine, you need to know which version of JDK is currently in use, use the parameter -version to know the version, command behavior:


java -version

Increase the maximum memory that the virtual machine can use

              The maximum memory available for the Java virtual machine is limited; the default is usually 64MB or 128MB. If an application loads data into Memory to improve performance and takes up a large amount of Memory, such as 128MB more than the default maximum, the maximum Memory available to the Java virtual machine needs to be increased or an Out of Memory exception will occur. When starting Java, you need to use the following two parameters:
              - the amount of memory used when the Xms Java virtual machine is initialized
              - maximum memory available for the Xmx Java virtual machine
              The size set in the above two parameters can have units, for example: 256m represents 256MB

Examples:


java -Xms128m -Xmx256m ...

Represents 128MB of memory used when the Java virtual machine is initialized and a maximum of 256MB is available.

For tomcat, you can modify its script catalina.sh (Unix platform) or catalina.bat (Windows platform) by setting the variable JAVA_OPTS, for example:


JAVA_OPTS='-Xms128m -Xmx256m'

In the console output, there is a command -x (note the uppercase), which is the command to view the JVM configuration parameters.

Next, use the java-x command to view the JVM's configuration instructions:
1. -xmixed mixed mode execution (default)
  Mixed mode execution
 
2. -Xint performance mode performance only
  Interpreted pattern execution
 
3, - Xbootclasspath: < Directories and zip/jar files separated by; >
          Set search path for bootstrap classes and resources
  Set the directory path for the zip/jar resource or class (.class file)
 
3, - Xbootclasspath/a: < Directories and zip/jar files separated by; >
          Append to end of bootstrap class path
  Append zip/jar resources or classes (.class files) to directory paths
 
4, - Xbootclasspath/p: < Directories and zip/jar files separated by; >
          Prepend in front of bootstrap class path
  Preload the zip/jar resource or class (.class file) directory path
 
5. -Xnoclassgc disable class garbage collection
  Turn off class garbage collection
 
6. -xincgc enable incremental garbage collection
  Enable garbage collection for classes
 
7, - Xloggc: < File> Log GC status to a file with time stamps
  Logs garbage back to a file.
 
8. -Xbatch disable background compilation
  Turn off background compilation
 
9, - Xms< Size> Set initial Java heap size
  Sets the JVM initialization heap memory size
 
10 - Xmx< Size> Set maximum Java heap size
  Sets the maximum heap memory size of the JVM
 
11, - Xss< Size> Set Java thread stack size
  Set the JVM stack memory size
 
12. -xprof output CPU profiling data
  Enter the CPU profile table data
 
13, - Xfuture enable strictest checks, anticipating the future default
  Perform a rigorous code review to anticipate possible scenarios
 
-xrs reduce use of OS signals by Java/VM (see documentation)
  Restore operating system signals through the JVM
 
-xcheck :jni perform. Additional checks for jni functions
  Perform a check on the JNI function
 
16, -xshare :off do not attempt to use Shared class data
  Use Shared class data as little as possible
 
17. -xshare :auto use Shared class data if possible (default)
  Use Shared class data whenever possible
 
18, -xshare :on require using Shared class data, otherwise fail.
  Use Shared class data whenever possible, or the run will fail

How do you use these parameters? In fact, all command lines are used in this way, so I will give a simple example of how to use this parameter, HelloWorl, very simple.


// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}

Compile and run:
D: \ j2sdk15 \ bin> Javac HelloWorld. Java
D: \ j2sdk15 \ bin> Java - Xms256M - Xmx512M HelloWorld
Hello World!

That's all for this article, and I hope you have a new understanding of how to run a class file in Java.


Related articles: