简体   繁体   English

(springboot)jar无法在其他计算机上执行

[英](springboot) jar not executable on other computers

I have a spring Boot application. 我有一个Spring Boot应用程序。 I package it with maven, and I execute it with java -jar xxx.jar. 我用maven打包,并用java -jar xxx.jar执行它。 The application is running. 该应用程序正在运行。 However if I copy the jar on another machine (same java version, same OS) I get a dependency error: 但是,如果将jar复制到另一台机器(相同的Java版本,相同的操作系统)上,则会出现依赖项错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration':...
...
Unsatisfied dependency expressed through constructor parameter 0 
...
Bean instantiation via factory method failed
...
 Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [javax.sql.DataSource]: 
Factory method 'dataSource' threw exception; nested exception is java.lang.NullPointerException

I added debug=true to show the classpath during execution, but it seems the same on both machines (there were issues with classpath order between maven execution and java execution) 我添加了debug = true以在执行期间显示类路径,但是两台机器上似乎都一样(maven执行和Java执行之间的类路径顺序存在问题)

EDIT: actually the problem is not changing machine: if I just move the jar in another folder, I get the same error. 编辑:实际上问题不在于更换机器:如果我只是将罐子移到另一个文件夹中,则会收到相同的错误。 If I look at the correct running process it seems fairly obvious that in the classpath there are references to ther project target folder that cannot be satisfied... 如果我看正确的运行过程,则很明显在类路径中有对无法满足的项目目标文件夹的引用。

Original machine is iOS, I tried the jar on other iOS and a Centos machine with same result. 原始机器是iOS,我在其他iOS和Centos机器上尝试了jar,结果相同。

I thought jar was very portable for deployment, so I don't understand if there is a better way of deploying the application or some environment variable I'm not taking into account 我以为jar可以很容易地部署,所以我不知道是否有更好的方法来部署应用程序或某些我没有考虑的环境变量

EDIT: I use maven. 编辑:我使用Maven。 I do 我做

mvn package

or mvn clean install 或mvn全新安装

and I have the dependencies mentioned in the answer 我有答案中提到的依赖项

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

You need to create a Fat jar (a jar file with all the dependencies), you can find a more detailed info here , but to let some code in the answer: 您需要创建一个Fat jar (具有所有依赖项的jar文件),您可以在此处找到更详细的信息,但要在答案中添加一些代码:

Basically what you have to do is check that you have the right dependencies in your pom 基本上,您要做的就是检查pom中是否具有正确的依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.1.RELEASE</version>
    </dependency>
</dependencies>

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.0.1.RELEASE</version>
    </plugin>
</plugins>

Then you should do 那你应该做

mvn clean install

And run it! 并运行它!

java -jar <artifact-name> 

EDIT 编辑

What do you see if you do a repackage? 重新包装后会看到什么? mvn clean package spring-boot:repackage

Your HibernateJpaConfiguration bean cannot be created. 您的HibernateJpaConfiguration bean无法创建。 It looks it has some property declared in a @Configuration annotated class or in the application.properties resource file. 看起来它具有在@Configuration注释的类或application.properties资源文件中声明的某些属性。 Some property that has a relative path in it, and when you move the jar it cannot find that property. 某些具有相对路径的属性,当您移动jar时,找不到该属性。

Actually it was a trivial issue: a directory had to be listed from a relative path and in order to execute the jar somewhere else I had to just create that directory. 实际上,这是一个琐碎的问题:必须从相对路径中列出目录,然后才能在其他地方执行jar,我只需要创建该目录即可。

The error wasn't properly catched so the stacktrace was showing failure in dependencies and I missed the simple reality: java.lang.NullPointerException.... 错误未正确捕获,因此stacktrace在依赖项中显示失败,因此我错过了简单的现实:java.lang.NullPointerException ....

Thanks to @AndyWilkinson for making me read the stacktrace again... 感谢@AndyWilkinson让我再次阅读stacktrace ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM