Java equivalent to app.config?

Is there a Java equivalent to .NET's App.Config?

If not is there a standard way to keep you application settings, so that they can be changed after an app has been distributed?


Asked by: Lily751 | Posted: 23-01-2022






Answer 1

For WebApps, web.xml can be used to store application settings.

Other than that, you can use the Properties class to read and write properties files.

You may also want to look at the Preferences class, which is used to read and write system and user preferences. It's an abstract class, but you can get appropriate objects using the userNodeForPackage(ClassName.class) and systemNodeForPackage(ClassName.class).

Answered by: Thomas980 | Posted: 24-02-2022



Answer 2

To put @Powerlord's suggestion (+1) of using the Properties class into example code:

public class SomeClass {
    public static void main(String[] args){
        String dbUrl = "";
        String dbLogin = "";
        String dbPassword = "";     
        if (args.length<3) {
            //If no inputs passed in, look for a configuration file
            URL configFile = SomeClass.class.getClass().getResource("/Configuration.cnf");
            try {
                InputStream configFileStream = configFile.openStream();
                Properties p = new Properties();
                p.load(configFileStream);
                configFileStream.close();

                dbUrl      = (String)p.get("dbUrl");
                dbLogin    = (String)p.get("dbUser");
                dbPassword = (String)p.get("dbPassword");               
            } catch (Exception e) {  //IO or NullPointer exceptions possible in block above
                System.out.println("Useful message");
                System.exit(1);
            }
        } else {
            //Read required inputs from "args"
            dbUrl      = args[0];
            dbLogin    = args[1];
            dbPassword = args[2];           
        }
        //Input checking one three items here
        //Real work here.
    }
}

Then, at the root of the container (e.g. top of a jar file) place a file Configuration.cnf with the following content:

#Comments describing the file
#more comments
dbUser=username
dbPassword=password
dbUrl=jdbc\:mysql\://servername/databasename

This feel not perfect (I'd be interested to hear improvements) but good enough for my current needs.

Answered by: Freddie485 | Posted: 24-02-2022



Answer 3

The simple way is to simply have a properties file, e.g., myapp.properties, with all your settings in. It's not a very advanced way to do settings but it suffices, or you can have your own XML based setup, or get them from a database, etc.

Answered by: Rebecca371 | Posted: 24-02-2022



Similar questions

configuration - What is the Java equivalent of .NET's Enterprise Library?

I am a .NET guy but recently have been placed on a Java project. Is there a java equivalent of .NET's Enterprise Library? More specifically, I only need a configuration manager, data access helper (parametrization and sanitize), and maybe a logger. What are the hot tools for java development?


java - In Netbeans how do you set the equivalent of Eclipse's run/debug configuration arguments?

I've inherited a java app that uses a XML as input. From the command line it runs like this: java -jar myJar.jar -f /path/to/my/xmlfile.xml In eclipse you can set run/debug config args from the Run->Debug Configurations... (arguments tab). How do you set the equivalent in Netbeans? Don't know if it makes a difference but it's a Netbeans maven project. thanks!


java - what's the equivalent code based configuration to add log4j in spring MVC 3.0+

I am migrating my Spring MVC project to code based configuration, but not sure how to add a listener, here's what in my web.xml &lt;context-param&gt; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; &lt;param-value&gt;WEB-INF/spring/log4j.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/l...


java - Equivalent for statement <value/> in spring xml configuration

I find this spring xml configuration: &lt;bean class="..." id="..."&gt; &lt;property name="..."&gt; &lt;array&gt; &lt;value/&gt; &lt;/array&gt; &lt;/property&gt; &lt;/bean&gt; Is &lt;value/&gt; statement the same with null value? Or with empty value ""?


Java JMS codes that are equivalent to the following mule JMS connector configuration?

Have to send json message to this closed-box mule system with Java, through a message broker of course (MQ/Solace). But somehow the system won't parse the json string upon receiving. After some query, all I get is this sample mule JMS config GUI snippet on publishing to the mule system. So hopefully I can follow this and create the expected JMS message.


networking - How to get the system HTTP proxy configuration in Java

This question already has answers here:


java - How to tune Tomcat 5.5 JVM Memory settings without using the configuration program

I need to configure Tomcat memory settings as part of a larger installation, so manually configuring tomcat with the configuration app after the fact is out of the question. I thought I could just throw the JVM memory settings into the JAVA_OPTS environment variable, but I'm testing that with jconsole to see if it works and it... doesn't. As per the comment below, CATALINA_OPTS doesn't work either. So far, the only...


java - Embed XML configuration directly in Spring application context

I have a Java object which is able to configure itself given an XML configuration description (it takes other descriptions as well, but I'm interested in the XML at the moment). I'm wondering if I can embed the XML description directly into a Spring application context description. I'm imagining something like: &lt;bean id="myXMLConfiguredBean" class="com.example.Foo"&gt; &lt;constructor-arg type="xml"&gt...


How to launch multiple Java programs with one configuration on separate consoles (with Eclipse)

I'm working with a Java program that has multiple components (with Eclipse &amp; Ant at the moment). Is there some way to start multiple programs with one launch configuration? I have an Ant target that does the job (launches multiple programs) but there are things I would like to do: I would like to debug the programs with Eclipse, hence the need for Eclipse launch. I would like to see the...


java - Netbeans and external configuration files

I am developing a Java desktop application and would like to have an external configuration.xml. I am developing the application using Netbeans and tried to add the configuration.xml file in the dist directory so that it resides in the application work folder. But when Netbeans executes its clean operation it deletes the dist directory, Where should I put this configuration.xml file so that it will not be deleted a...


encryption - Java - encrypt / decrypt user name and password from a configuration file

We are busy developing a Java web service for a client. There are two possible choices: Store the encrypted user name / password on the web service client. Read from a config. file on the client side, decrypt and send. Store the encrypted user name / password on the web server. Read from a config. file on the web server, decrypt and use in the web service. The user name /...


java - spring bean configuration

I want to specify a file system path as part of a Spring bean configuration. I know that I can set a path such as: &lt;bean id="myBean" class="com.example.BeanImpl"&gt; &lt;property name="path" value="/WEB-INF/jsp"/&gt; &lt;/bean&gt; An the path /WEB-INF/jsp is interpreted as being relative to the web application root. But how do I specify a path relative to file system root, e.g. /usr/...


java - What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto I need to know when to use the update and when not? And what is the alternative? These are changes that could happen over DB: new tables new columns in old tables columns deleted data type of a column changed a type of a column changed it...


configuration - Best way to alter constants in Java build process

I have inherited a Java application (servlets) that runs under Tomcat. For historical reasons, the code has different "look and feel" options based on where the application will be deployed (essentially a matter of branding). There are several constants that control this branding process, which have different functions and should not be compacted into a single constant (i.e. BRAND, MULTI-LANGUAGE, plus the location...


java - Should I implement source control for j2ee application server configuration files?

For a typical J2EE web application, the datasource connection settings are stored as part of the application server configuration. Is there a way to version control these configuration details? I want more control on the datasource and other application server config changes. What is the standard practice for doing this?






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top