java命令执行jar包(里面的main函数,无web.xml文件)的方式(包括依赖其它的jar包),使用Google-Guava Concurrent包里的Service框架,maven工程

本文详细介绍了如何使用Maven的shade插件来配置pom.xml文件,从而实现将Java应用程序打包为一个可直接运行的Jar包。此外,还展示了如何通过指定main-class属性来设置程序的入口,并给出了具体的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml中的配置

<span style="white-space:pre">			</span><plugin>
			        <groupId>org.apache.maven.plugins</groupId>
			        <artifactId>maven-shade-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>cn.sh.ideal.imr.boot.Bootstrap</mainClass>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
							</transformers>
							<filters>
								<filter>
									<artifact>*:*</artifact>
									<excludes>
										<exclude>META-INF/*.SF</exclude>
										<exclude>META-INF/*.DSA</exclude>
										<exclude>META-INF/*.RSA</exclude>
									</excludes>
								</filter>
							</filters>
						</configuration>
					</execution>
				</executions>
			</plugin>


大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口。

java代码

import java.io.FileNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer;

import com.google.common.util.concurrent.AbstractIdleService;

public class Bootstrap extends AbstractIdleService {
	private static Logger log = LoggerFactory.getLogger(Bootstrap.class);
    private ClassPathXmlApplicationContext context;
    
    static {
		try {
			Log4jConfigurer.initLogging("classpath:conf/log4j.properties");
		} catch (FileNotFoundException ex) {
			System.err.println("Cannot Initialize log4j");
		}
	}
    
    public static void main(String[] args) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.startAsync();
        try {
            Object lock = new Object();
            synchronized (lock) {
                while (true) {
                    lock.wait();
                }
            }
        } catch (InterruptedException ex) {
            log.error("ignore interruption");
        }
    }

    @Override
    protected void startUp() throws Exception {
        context = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml"});
        context.start();
        context.registerShutdownHook();
        log.info(" service started successfully!");
    }

    @Override
    protected void shutDown() throws Exception {
        context.stop();
        log.info(" service stop successfully!");
    }
}

用maven打包后,部署发布,执行java -jar 项目名.jar命令,启动jar包


参考文章:

http://xinklabi.iteye.com/blog/2157591


Servlet 3 + Spring MVC零配置:去除所有xml

http://blog.youkuaiyun.com/xiao__gui/article/details/46803193


Google-Guava Concurrent包里的Service框架浅析

http://ifeve.com/google-guava-serviceexplained/


浅谈java 执行jar包中的main方法

http://www.jb51.net/article/92712.htm




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值