Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot无法在Linux上解析占位符,在Eclipse上可以正常工作_Java_Spring Boot - Fatal编程技术网

Java SpringBoot无法在Linux上解析占位符,在Eclipse上可以正常工作

Java SpringBoot无法在Linux上解析占位符,在Eclipse上可以正常工作,java,spring-boot,Java,Spring Boot,我的SpringBoot 2.0.3 java 8程序在Eclipse光子上运行良好(使用Maven),但是,当我将其构建到jar文件中并安装在AWS Linux上并在那里运行时,我得到: 2019-06-10 10:07:14.395 WARN 18316 --- [ main]s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - c

我的SpringBoot 2.0.3 java 8程序在Eclipse光子上运行良好(使用Maven),但是,当我将其构建到jar文件中并安装在AWS Linux上并在那里运行时,我得到:

2019-06-10 10:07:14.395  WARN 18316 --- [           main]s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccWrapperApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'tmp.dir' in value "${tmp.dir}"
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ccWrapperApplication': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'tmp.dir' in value "${tmp.dir}"
因此,在Linux上运行时,spring似乎找不到application.properties变量。为什么不呢

tmp.dir在src/main/resources/application.properties中定义为:

tmp.dir=tmp
ccWrapperApplication.java具有:

@SpringBootApplication
// SpringBoot likes to use a database, we don't need one, so we exclude the default dB.
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

public class CcWrapperApplication implements CommandLineRunner {

    @Value("${tmp.dir}")
    public  String tmpDir;

    public static void main(String[] args) {
        logger.info("running CcWrapperApplication on " + System.getProperty("os.name"))
        SpringApplication.run(CcWrapperApplication.class, args);
    }

    @Override
    public void run(String... args) {
    ...
    }

    public String getTmpDir() {
        return tmpDir;
    }

    public void setTmpDir(String tmpDir) {
        this.tmpDir = tmpDir;
    }

}
我使用eclipse构建了应用程序jar: 导出->可运行JAR文件

在Linux上安装jar,将jar添加到类路径(以及所有SpringBoot jar),并使用以下命令从shell脚本运行:

java  com.clarivate.singulatiry.chemworkbench.chemcurator.chemcurator_wrapper.CcWrapperApplication $inputDir $outputDir 2>&1 >>$logfile | tee -a $logfile >>$errorfile

感谢您的帮助。

我按照@Malathi的建议使用mvn安装,并使用此处建议的方法创建了一个瘦jar:
虽然我仍然不知道最初的方法失败的原因,但它工作得很好。

Eclipse
生成
jar
的内置导出器只包括项目中实际生成的目标文件夹文件

为了拥有一个“胖”、“瘦”(独立可执行)的jar/war,您应该使用
springbootmaven
Gradle
插件来“重新打包”jar/war


可以进一步帮助您。

当您运行
mvn clean install
时,Spring boot会构建一个完全可执行的jar及其所有依赖项。为什么不将jar作为java-jar运行呢。我敢打赌,某些依赖项可能已丢失,因此属性未加载。但是,当您尝试使用mvn-Dhttps.protocols=TLSv1.2 clean install-DskipTests构建时,Eclipse会添加所有正确的依赖项集,然后运行jar文件。看起来缺少一些依赖项,并给出了tmp.dir的确切路径/tmp@malathi我尝试了你的建议,但是在jar文件中:BOOT-INF/classes/被添加到所有*.class条目的类路径之前,所以我得到了一个“找不到或加载主类”错误。你不能两者都有。您必须创建胖JAR才能在IDE之外运行它。SpringBootMaven插件知道如何正确创建可执行JAR。你一定是做错了什么事。是的,我知道。“占用大量空间”-不再是问题。磁盘空间很便宜。您需要这些jar是独立的和解耦的。不建议您使用a/lib文件夹和类路径。将所有内容放在common/lib文件夹中意味着,如果升级一个JAR,就必须升级每个服务。对不起,你用的是90年代的思维方式。它仍然是实验性方案的一部分,也许还没有完全成熟