在本文中,我们将学习如何在Linux环境下部署和运行一个jar包项目,jar包是Java应用程序的一种打包格式,它将所有的类文件、资源文件和元数据打包成一个可执行的文件,在Linux环境下,我们可以使用Java命令来运行jar包,或者使用Apache Maven等构建工具来自动化部署过程。
环境准备
1、安装Java环境
我们需要在Linux系统上安装Java环境,可以通过以下命令来安装OpenJDK:
sudo apt-get update sudo apt-get install openjdk-8-jdk
2、安装Maven
Maven是一个用于项目管理和构建自动化的工具,它可以帮助我们更方便地部署和管理jar包项目,可以通过以下命令来安装Maven:
sudo apt-get install maven
3、创建Maven项目
接下来,我们需要创建一个Maven项目,可以通过以下命令来创建一个新的Maven项目:
mvn archetype:generate -DgroupId=com.example -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
这将在当前目录下创建一个名为myapp的Maven项目,进入项目目录:
cd myapp
4、编写Java代码
在myapp项目中,我们需要编写一个简单的Java程序,在src/main/java目录下,创建一个名为HelloWorld.java的文件,并添加以下内容:
package com.example; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
5、编译和打包jar包
为了将Java程序打包成jar包,我们需要在项目根目录下创建一个名为pom.xml的文件,并添加以下内容:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myapp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>myapp Maven Webapp</name> <url>http://example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
接下来,我们需要使用Maven命令来编译和打包项目,在项目根目录下执行以下命令:
mvn clean install
这将生成一个名为myapp-1.0-SNAPSHOT-jar-with-dependencies.jar的文件,其中包含了编译后的Java程序以及所有依赖的库。
运行jar包项目并查看输出结果
现在我们已经成功地将Java程序打包成了jar包,接下来我们可以使用Java命令来运行这个jar包,在终端中执行以下命令:
java -jar myapp-1.0-SNAPSHOT-jar-with-dependencies.jar > output.txt & echo $! > app.pid && tail -f output.txt & disown %1% %2% && kill $! && wait $! || echo "An error occurred while running the jar file" >&2; exit 1; exec bash; exit $?; exec bash; exit $?; exit $?; exec bash; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exit $?; exec bash >&2 & disown %1% && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1 && kill $$ && exec bash || echo "An error occurred while running the jar file" >&2 && sleep 1
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/223491.html