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

后端 未结 4 641
情深已故
情深已故 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:02

    Why not just invoke the application without specifying -jar and instead name the application main class explicitly? This will allow you to put both your new-config and the application.jar on the classpath in the required order:

    e.g. (assuming "new-config" is a directory containing the overridden properties file)

    java -cp new-config:application.jar Application.Main.Class
    

    I believe the name of main class can be found in the MANIFEST.MF file inside the jar ....

    0 讨论(0)
  • 2020-12-18 20:07

    The JAR archive specified by the -jar option, overrides all other values.

    You would have to generally do it with an outside config file or build your own solution withod ClassLoader.getResource().

    We use a custom solution to solve this - we load the internal properties like so:

    final Properties p = new Properties();
    p.load(DefaultConfiguration.class.getResourceAsStream("config.properties"));
    

    We then load the external file in the same way and overwrite the internal values with the external ones.

    For info on how class loading works see:

    http://java.sun.com/javase/6/docs/technotes/tools/findingclasses.html

    0 讨论(0)
  • 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

    <bean id="MyDataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
        <property name="url" value="jdbc:microsoft:sqlserver://${dbServer}:${dbPort};DatabaseName=${dbName}"/>
        <property name="username" value="${dbUserName}"/>
        <property name="password" value="${dbPassword}"/>
        <property name="suppressClose" value="false"/>
    </bean>
    

    Property File Snippet

    dbServer=MyServer
    dbPort=1433
    dbName=MyDb
    dbUserName=Me
    dbPassword=foobar
    
    0 讨论(0)
  • 2020-12-18 20:22

    It may not be possible using just the CLASSPATH. There are ways to get make the call to ClassLoader.getResource() use a static path to find the resource. If it is doing that, it is bypassing the CLASSPATH.

    0 讨论(0)
提交回复
热议问题