gpt4 book ai didi

java - Spring可运行jar在窗口上运行

转载 作者:行者123 更新时间:2023-12-01 12:41:10 25 4
gpt4 key购买 nike

我创建了一个项目,该项目是在 Eclipse 上开发为 spring mvn 项目,但后来不得不添加更多通过 mvn 未添加的 jar,所以我收集了文件夹中的所有 jar,并通过 eclipse 创建了一个可运行的 jar 并尝试从命令提示符运行,它显示以下错误并且它正在 eclipse 中运行:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.
StandardEnvironment).`enter code here`
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinit
ionParsingException: Configuration problem: Unable to locate Spring NamespaceHan
dler for XML schema namespace [http://www.springframework.org/schema/context]
Offending resource: class path resource
at org.springframework.beans.factory.parsing.FailFastProblemReporter.err
or(FailFastProblemReporter.java:68)

最佳答案

我建议使用 Spring Boot 来避免这个问题。使用 Spring 创建可运行的 jar 是一件令人头疼的事情,主要是因为 spring.schemasspring.handler 文件重叠(它们负责查找 NamespaceHandlers >)。当您构建单个 jar 时,Maven 将覆盖这些文件。

但现在,您可以查看 Shade Plugin并使用ResourceTransformers

Aggregating classes/resources from several artifacts into one uber JAR is straight forward as long as there is no overlap. Otherwise, some kind of logic to merge resources from several JARs is required. This is where resource transformers kick in.

来自 Shade 插件网站的代码(后跟注释)

  <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</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>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Some jars contain additional resources (such as properties files) that have the same file name. To avoid overwriting, you can opt to merge them by appending their content into one file. One good example for this is when aggregating both the spring-context and plexus-spring jars. Both of them have the META-INF/spring.handlers file which is used by Spring to handle XML schema namespaces. You can merge the contents of all the files with that specific name using the AppendingTransformer

关于java - Spring可运行jar在窗口上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com