在花费大量时间尝试解决此问题之后,终于找到了可行的解决方案。该解决方案利用了Spring的ResourceUtils。也应该适用于json文件。
package utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.File;
public class Utils {
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class.getName());
public static Properties fetchProperties(){
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return properties;
}
}
要回答有关评论的一些问题:
可以肯定地说,我已经在Amazon EC2上使用 java -jar target/image-service-slave-1.0-SNAPSHOT.jar