Can I use the classpath to override a file in a jar that is being run?

后端 未结 4 649
情深已故
情深已故 2020-12-18 19:45

I have a JAR file that contains an application as well as configuration files for that application. The application loads configuration files from the classpath (using

4条回答
  •  生来不讨喜
    2020-12-18 20:19

    When you use the -jar option to launch your application:

    ... the JAR file is the source of all user classes, and other user class path settings are ignored.

    as described here. A workaround would be to specify the classpath in the jar file's manifest to include the additional path (described here).

    However, given that you are only talking about amending configuration you may want to take a different approach that is not reliant on the classpath. For example, I typically configure my applications through Spring using property files to determine the location of databases, etc. My Spring configuration is consistent across test, QA and live environments but I pass a different property file as a command line argument when launching the app.

    Spring Configuration Snippet

    
        
        
        
        
    
    

    Property File Snippet

    dbServer=MyServer
    dbPort=1433
    dbName=MyDb
    dbUserName=Me
    dbPassword=foobar
    

提交回复
热议问题