当前位置: 首页 > 知识库问答 >
问题:

尝试从jar文件运行时,Spring boot应用程序不会运行

严修诚
2023-03-14

我有一个Spring启动应用程序,当通过intellij运行时工作正常。但是当我从jar运行它时,我得到了下面的异常。

        2017-02-13 05:18:28.596  WARN 8581 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    2017-02-13 05:18:28.606 ERROR 8581 --- [           main] o.s.boot.SpringApplication               : Application startup failed

    org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at com.testservice.challenge.testserviceServiceApplication.main(testserviceServiceApplication.java:13) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [bus-route-service-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [bus-route-service-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [bus-route-service-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [bus-route-service-0.0.1-SNAPSHOT-exec.jar:0.0.1-SNAPSHOT]
    Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:117) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:84) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:537) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:179) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        ... 16 common frames omitted
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationConfig.JerseyConfig' defined in URL [jar:file:/home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes!/com/testservice/challenge/ApplicationConfig$JerseyConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.testservice.challenge.ApplicationConfig$JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes (No such file or directory)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:234) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:215) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:91) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:79) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getServletContextInitializerBeans(EmbeddedWebApplicationContext.java:241) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:228) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:89) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:213) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55) ~[spring-boot-1.5.1.RELEASE.jar!/:1.5.1.RELEASE]
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5178) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_111]
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationConfig.JerseyConfig' defined in URL [jar:file:/home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes!/com/testservice/challenge/ApplicationConfig$JerseyConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.testservice.challenge.ApplicationConfig$JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes (No such file or directory)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        ... 35 common frames omitted
    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.testservice.challenge.ApplicationConfig$JerseyConfig]: Constructor threw exception; nested exception is org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes (No such file or directory)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        ... 47 common frames omitted
    Caused by: org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: /home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes (No such file or directory)
        at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:89) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:65) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.addResourceFinder(PackageNamesScanner.java:282) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.init(PackageNamesScanner.java:198) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:154) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.PackageNamesScanner.<init>(PackageNamesScanner.java:110) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:680) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.ResourceConfig.packages(ResourceConfig.java:660) ~[jersey-server-2.25.1.jar!/:na]
        at com.testservice.challenge.ApplicationConfig$JerseyConfig.<init>(ApplicationConfig.java:24) ~[classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        ... 49 common frames omitted
    Caused by: java.io.FileNotFoundException: /home/mayooran/Projects/bus-route-challenge/target/bus-route-service-0.0.1-SNAPSHOT-exec.jar!/BOOT-INF/classes (No such file or directory)
        at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_111]
        at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_111]
        at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_111]
        at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[na:1.8.0_111]
        at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) ~[na:1.8.0_111]
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) ~[na:1.8.0_111]
        at java.net.URL.openStream(URL.java:1045) ~[na:1.8.0_111]
        at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.getInputStream(JarZipSchemeResourceFinderFactory.java:177) ~[jersey-server-2.25.1.jar!/:na]
        at org.glassfish.jersey.server.internal.scanning.JarZipSchemeResourceFinderFactory.create(JarZipSchemeResourceFinderFactory.java:87) ~[jersey-server-2.25.1.jar!/:na]
        ... 62 common frames omitted

下面是我的pom文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testservice.challenge</groupId>
    <artifactId>test-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test-service</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-reflect</artifactId>
            <version>1.6.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
            <version>1.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                 <configuration>
                    <classifier>exec</classifier>
                    <mainClass>com.testservice.challenge.TestServiceApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>springloaded</artifactId>
                        <version>1.2.6.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>


</project>

我试着用两个

mvn clean package 

mvn clean install spring-boot:repackage

然后我通过java运行jar-jar-DParamName="有些Param"

我做错了什么?我是Spring的新手。任何帮助都将不胜感激。

共有3个答案

易博文
2023-03-14

为了补充@Bampfer的答案,因为谷歌把我带到这里。这是一种不必在单独的模块中解压资源并进行动态组件扫描的方法。

创建一个自定义注释,将其放在任何资源类上。

public @interface JaxRsResource {
}
@Named
@JaxRsResource
@Path("/my/api/path")
public class MyResource {
    @GET
    public String helloWorld() { return "Hello World"; }
}

然后,我使用了这篇文章中的Spring组件扫描api:在运行时扫描Java注释,并用我的自定义注释注册了我找到的组件,并调用了寄存器函数。

@Configuration
public class JaxrsApplication extends ResourceConfig {

    private final Logger logger = LoggerFactory.getLogger(JaxrsApplication.class);

    public JaxrsApplication() {

        final ClassPathScanningCandidateComponentProvider scanner =
                new ClassPathScanningCandidateComponentProvider(false);

        scanner.addIncludeFilter(new AnnotationTypeFilter(JaxRsResource.class));

        for (final BeanDefinition resourceBean : scanner.findCandidateComponents("my.base.package")) {
                try {
                    final Class resourceClass = getClass().getClassLoader().loadClass(resourceBean.getBeanClassName());
                    register(resourceClass);
                } catch (final ClassNotFoundException e) {
                    logger.error(String.format("Unable to load Jax-RS resource {}.", resourceBean.getBeanClassName()), e);
                }
            }
        }
    }
}

注意:您必须从类加载器加载类,并将类对象传递给register()。如果只将类名作为字符串传递给register方法,则不会加载资源。

云丰
2023-03-14

@阿巴格尔的答案有正确的解释,也可以说是最好的解决方案。

由于Jersey的扫描步骤是失败的,另一种解决方法是显式注册每个资源。我替换了这个:

@Component
public JerseyConfig() extends ResourceConfig {
    packages("com.mydomain.resource");
}

具有

@Component
public JerseyConfig() extends ResourceConfig {
    register(TimeResource.class);
    register(SpaceResource.class);
    // etc.
}

(使用Spring Boot 2.1.3测试)

我的代码有大约25个泽西的资源类要注册。这突出了这种方法的缺点,即必须维护显式注册。不会自动找到新的资源类,并且可能会遇到令人困惑的错误。

另一方面,您不需要拆分项目。因此,显式注册有时可能已经足够好了。

谢和颂
2023-03-14

您的pom.xml.中有spring-boot-starter-jersey您得到的错误是/BOOT-INF/class(没有这样的文件或目录)。这是jersey的问题,它不能很好地与spring-boot胖jar配合使用。请参考这个和这个以获取更多详细信息。为了解决这个问题,您必须将应该由jersey扫描的类打包到jar中,并按照Spring-Boot留档中给出的方式提取jar当可执行jar运行时提取特定库

 类似资料:
  • 当我使用 在intelij运行期间,一切正常。 在jar的案例中,我尝试了: 等等。它们导致正确的路径,例如: > 文件:/D:/Projects/myProject/target/classes/static/assets/config/fileName(对于toExternalForm) /D: /Projects/myProject/target/classes/static/assets/

  • 当我试图通过控制台通过jar文件运行应用程序时,在登录事件期间,它会给出一个异常“java.lang.NullPointerException:Location is required”。但当我使用intellij IDE运行应用程序时,它工作得很好。 它给出了行的例外情况: 我也尝试给出fxml文件的绝对路径。它只在我使用jar文件运行应用程序时发生。jar中也没有文件丢失。这可能是什么原因造成

  • 问题内容: 我刚刚开始学习Java,并且只知道少量的代码,但是,这仍然是一个简单的程序。它更多是一个恶作剧程序,但主要只是为了测试我是否可以创建一个jar文件。 这是代码: 我将其保存到名为的文件中,然后使用进行编译 这有效,当我使用运行它时 它也可以正常工作。 因此,然后我尝试创建一个jar文件。我使用命令 而且有效。之后,我双击jar文件,它出现一个错误。 所以我在cmd中运行它 并得到这个错

  • 我是Grails的新手,我用GVM工具安装了它,并试图从终端运行一个默认应用程序,只需尝试以下命令:Grails create-app demo和Grails run-app,我得到了一个巨大的错误: 编辑:问题解决了,只需升级JDK版本。

  • 首先,抱歉英语不好。我从Flatter开始,我已经安装了所有必要的东西,但是当尝试运行测试应用程序(在模拟器或设备中)时,我会收到下面的错误消息。 正在启动lib\main。调试模式下XT1097上的dart。。。正在初始化gradle。。。正在解析依赖项…*运行Gradle时出错:从以下位置退出代码1:C:\Testes\first\u app\android\gradlew。bat应用程序:属