Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, you want to treat your <code>.properties</code> file on the same folder as the main/runnable jar as a file rather than as a resource of the main/runnable jar. In that case, my own solution is as follows:</p> <p>First thing first: your program file architecture shall be like this (assuming your main program is main.jar and its main properties file is main.properties):</p> <pre><code>./ - the root of your program |__ main.jar |__ main.properties </code></pre> <p>With this architecture, you can modify any property in the main.properties file using any text editor before or while your main.jar is running (depending on the current state of the program) since it is just a text-based file. For example, your main.properties file may contain:</p> <pre><code>app.version=1.0.0.0 app.name=Hello </code></pre> <p>So, when you run your main program from its root/base folder, normally you will run it like this:</p> <pre><code>java -jar ./main.jar </code></pre> <p>or, straight away: </p> <pre><code>java -jar main.jar </code></pre> <p>In your main.jar, you need to create a few utility methods for every property found in your main.properties file; let say the <code>app.version</code> property will have <code>getAppVersion()</code> method as follows:</p> <pre><code>/** * Gets the app.version property value from * the ./main.properties file of the base folder * * @return app.version string * @throws IOException */ public static String getAppVersion() throws IOException{ String versionString = null; //to load application's properties, we use this class Properties mainProperties = new Properties(); FileInputStream file; //the base folder is ./, the root of the main.properties file String path = "./main.properties"; //load the file handle for main.properties file = new FileInputStream(path); //load all the properties from this file mainProperties.load(file); //we have loaded the properties, so close the file handle file.close(); //retrieve the property we are intrested, the app.version versionString = mainProperties.getProperty("app.version"); return versionString; } </code></pre> <p>In any part of the main program that needs the <code>app.version</code> value, we call its method as follows:</p> <pre><code>String version = null; try{ version = getAppVersion(); } catch (IOException ioe){ ioe.printStackTrace(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload