解决linux环境下项目无法读取jar包 外配置 的问题

本地读取:application.yml配置 文件的绝对路径

 linux环境读取:建立文件夹将配置文件置于同jar包同级的目录,并获取绝对路径

将路径配置如上图配置文件中,打jar包。

代码获取配置文件:

@Component
public class SqliteDbUtils {
    private static Connection connection = null;
    private static String jdbc_prefix = "jdbc:sqlite:";
    private static String db_file = "project_server_info.db";
    private static String sql = "SELECT * FROM project_servers_info;";


    private static String resourcePath;

    @Value(value = "${db.dbpath}")
    private void setResourcePath(String  resourcePath){
        this.resourcePath = resourcePath ;
    }

    static {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            //String filePath = FileUtils.class.getClassLoader().getResource(db_file).getPath();
            String filePath = resourcePath;
            System.out.println(filePath);
            connection = DriverManager.getConnection(jdbc_prefix.concat(filePath));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return connection;
    }
}

你可能感兴趣的:(IT,linux,jar,sqlite,java)