java导出可执行文件

java导出可执行文件

本文主要讲解java project如何导出可执行jar文件:


工程介绍

  • ** 首先要有一个可执行文件的入口,也就是main方法入口。要保证导出之前main()函数可以正常执行。

代码如下:

public class EntranceController {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        try {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            context.start();
            PassportService passportService = context.getBean("passportService", PassportService.class);
            System.out.println("------------ passportService---------:" + passportService);
        } catch (Exception e) {
            e.printStackTrace();
        }
        synchronized (EntranceController.class) {
            while (true) {
                try {
                    EntranceController.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这里写图片描述

配置文件在classpath 路径下,打成jar包,直接从根目录读取!

注意:maven项目,配置文件只能放在src/main/java目录下

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

<!-- 读取配置文件 -->
<context:property-placeholder location="setting.properties" />

导出流程

1.选中要打jar包的工程
2.鼠标右击,选择Export…
3.选择java中的Runnable JAR file(如图)

这里写图片描述

点击“Next”
4.
(1)在Launch configuration:选择要打jar包的mian所在的类名;(如图)
(2)在Export destination:选择要存放jar的名称和地址(如图)
(3)如果要打的jar包需要调用别的jar包 请选择Library handling:中copy required libraries into a sub-folder next to the generated JAR(如图)

这里写图片描述

5.点击 “Finish”

运行jar包

java -jar /opt/soft/aaa.jar

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值