Java -jar : access external configuration file

前端 未结 2 1585
无人共我
无人共我 2020-12-16 02:56

I\'m looking to do something which I thought was not going to be difficult.

I have an application that I\'d like to package up as a jar because I\'ve got ~30 depend

2条回答
  •  离开以前
    2020-12-16 03:25

    You need to add "." to the classpath of the jar file you are building for your application.

    So in the manifest, I'd expect to see

    Main-Class: some.full.Name
    Class-Path: . needed-lib.jar other-lib.jar 
    

    Then, when you run your app by executing

    java -jar myapp.jar
    

    it actually uses

    java -classpath .;needed-lib.jar;other-lib.jar some.full.Name
    

    This way any file in the directory with the myapp.jar file will also be on the classpath. That classpath is relative to the location of jar containing it.

    Log4j for one expects the log4j.xml configuration file to be on the classpath. If you aren't using the name log4j.xml you also have to add a system property to your start up command to tell the log4j library to look for a different name.

    I'm not sure what Spring expects for configuration files. And property file loading depends on what mechanism is used to load the file. Using a FileReader or InputStream doesn't use the classpath mechanism at all. In that case you need to know where the application is expecting the file to be relative to the current working directory.

提交回复
热议问题