Java entire compilation and running process needs to go through several key steps, this article through a simple program to simply illustrate the whole process.
This article is divided into two parts: one is to compile and run ordinary Java files, the other is to compile and run JAR files.
Compile and run ordinary Java files
1. Check the JDK environment
1) Check whether the Java version and Java tools are available
$> Java -version Java version "1.8.0_162"Java(TM) SE Runtime Environment (build 1.8.0_162-B12)Java HotSpot(TM) 64-bit Server VM (Build 25.162-B12, Mixed mode)Copy the code
2) Check whether the JavAC version and javAC tool are available
$> javac -version javac 1.8.0_162Copy the code
$> vim jInfoTest1.java $> vim jInfoTest1.java
/* Import java.io.ioexception; /* import java.io.ioexception; public class JinfoTest1 { public static void main(String[] args) { String arg1 = System.getProperties().getProperty("arg1"); System.out.println(" arg1: "+ arg1); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code
3. Compile Java files
$> javac -encoding UTF-8 JInfoTest1.java # generates jInfoTest1.class in the same directoryCopy the code
4, test run (runtime class names do not need to be suffix. Class)
JinfoTest1 = arg1; Nul# with arguments: use "-dxxx =yyy" as the command line option, pass the parameter XXX to main with the value yyy$> Java -darg1 =123 JinfoTest1 with the parameter arg1: 123# Note: If you want to run the *. Class file with the package package, you need to put the *. Class file in the corresponding package directory of the disk and specify the full path of the package at run time. Otherwise, an error occurs: the main class jinfo.jInfoTest1 cannot be found or loaded. The jInfoTest1.java file comes with the package com.daoge. $> mkdir -p com/daoge$> mv JinfoTest1.class com/daoge/$> java -Darg1=123 com.daoge.JinfoTest1Copy the code
2. Compile and run jar files
1. Check the JDK environment
1) Check whether the Java version and Java tools are available
$> Java -version Java version "1.8.0_162"Java(TM) SE Runtime Environment (build 1.8.0_162-B12)Java HotSpot(TM) 64-bit Server VM (Build 25.162-B12, Mixed mode)Copy the code
Create a maven project and add a dependency $> vim pom.xml
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.3 < / version > < relativePath / > < / parent > < the groupId > com. Daoge < / groupId > < artifactId > spring - the JVM < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name>spring-jvm</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build></project>Copy the code
3, new boot file $> vim. Com daoge. Spring. The JVM. SpringJvmApplication. Java
package com.daoge.spring.jvm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})public class SpringJvmApplication { public static void main(String[] args) { SpringApplication.run(SpringJvmApplication.class, args); System.out.println("spring boot is running..." ); String arg1 = System.getProperties().getProperty("arg1"); System.out.println(" arg1 :" + arg1); }}Copy the code
4. Package as jar files
Method 1: Package the IDEA or Eclipse plugin with Maven. Method 2: Use the MVN package command to package.
$> mvn package[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------
Copy the code
5. Test run
$> java-jar spring-jvm-0.0.1- snapshot. jar arg1 = $> java-jar spring-jvm-0.0.1- snapshot. jar arg1 = $> java-jar spring-jvm-0.0.1- snapshot. jar Nul# with arguments: use "-dxxx =yyy" as the command line option and pass the parameter XXX to main with the value yyy$> java-darg1 =123 -jar spring-Jm-0.0.1 -snapshot. jar arg1 is 123Copy the code
Author: Wechat public account — Dao Ge Said programming (RedCode1024) blogger, engaged in program development for more than 10 years, share practical technical solutions, welcome to leave a message.