Java封装jar包对外提供可执行文件
编写Main方法,封装jar包
1、编写Main方法
import java.util.Date;
/**
* 描述 :
*
* @Author : zhanghao
* @Time : 2019/10/28 14:08
*/
public class ***MainTest {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
***API ***ServiceAPI= (***ServiceAPI) applicationContext.getBean("****Impl");
System.out.println("定时任务执行结果_"+ DateUtils.format(new Date())+":"+***ServiceAPI.***Process());
}
}
具体项目结构如下:

2、通过idea可以快速进行Main方法的jar包封装,具体步骤可以参考
https://blog.csdn.net/yuang12345/article/details/90293013
https://blog.csdn.net/daiyunxing0545/article/details/85254303
https://blog.csdn.net/banjing_1993/article/details/83073210
3、编译完成后 执行 java -jar ****MainTest 即可运行相关方法
估计大概率,会执行报错(偷笑)
错误一:不识别java命令
检查是否正确配置了java的环境变量
错误二:***Test.jar中没有主清单属性
使用解压工具打开jar包,找到META-INF-->MANIFEST.MF->使用记事本打开,在第二行添加
Main-Class: main方法的路径
保存后重新执行命令即可
错误三:执行过程中报 Invalid signature file digest for Manifest main attributes
将打好包的jar文件中的 META-INF/*.RSA META-INF/*.DSA META-INF/*.SF 文件删掉
参考链接:https://blog.csdn.net/c1481118216/article/details/79667025
https://blog.csdn.net/dai451954706/article/details/50086295
错误四:执行过程中报 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
或者 其它的schema错误
由于没有配置META-INF/spring.handlers和META-INF/spring.schemas,所以如果工程中依赖了Spring的多个依赖,在打包时后面的会把前面的覆盖,使得这两个文件中永远只保存最后一个spring依赖的schema和handler。
方法一:使用maven的打包插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version> 1.7.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<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>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.chenzhou.test.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
方法二:
第二种方法其实很简单,找到这三个文件,用我提供的这三个完整的文件覆盖即可。
通常,这三个文件都在jar包下面的META-INF路径下
参考链接:https://www.iteye.com/blog/chenzhou123520-1971322
https://www.iteye.com/blog/chenzhou123520-1706242
https://www.cnblogs.com/starwater/p/6679777.html
错误五:找不到 ***.propertie或者spring-config.xml
maven打包插件配置错误,resources下面的配置没有打进来

浙公网安备 33010602011771号