java导出可执行文件

来源:互联网 发布:javascript生成随机数 编辑:程序博客网 时间:2024/04/30 12:10

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

这里写图片描述

0 0