user2821894 user2821894 - 20 days ago 9
Java Question

How to avoid hardcoding in java

I have read many articles on avoiding hardcoding in java. But could not get clear idea on how to apply it on my requirement.After doing some research am asking this question. Below is my code snippet. In that I want to avoid the hardcoding of the path name in Process pr = rt.exec().Any suggestions on how to do it.Thanks in advance.

public class StartUp {

String executable = getStringValue("executable.run");
String filein = getStringValue("incoming.file");
String params1 = getStringValue("executable.params1");
String params2 = getStringValue("executable.params2");
String log = getStringValue("log.file");
String ss = "Started";
public String startCommand() throws IOException, InterruptedException{



Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("C:\\server\\rd.exe -a C:\\file.lic -z[+]


// C:\File\log.txt");

Process pr = rt.exec(executable+" "+params1+" "+filein+" "+params2+" "+log);
BufferedReader input = new BufferedReader(new InputStreamReader
(pr.getInputStream()));

String line=null;
StringBuffer start= new StringBuffer();
while((line=input.readLine()) != null) {
start.append("ServerStarted" + line + "\n");
System.out.println(line);
}

int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
return line;



//return start.toString();
}
private static String getStringValue(String string) {
return string;

}
}

Answer

You have severals things to try:

java PROPERTIES

private Properties _properties;

private void init(){
     _properties = new Properties();
     InputStream configurationFileIS = PropertiesConfigurationHandler.class.getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
     _properties.load(configurationFileIS);
}

public String getStringValue(String path) {
    return _properties.getProperty(path);
}

and the properties file will be similar to

an.element.to.be.configured.like.a.path=/dev/null

But you can also use SPRING CONTEXT

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>WEB-INF/classes/config/properties/database.properties</value>
            <value>classpath:config/properties/database.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

and the elements inside database.properties will be accessed that way

"${jdbc.username}"

--

For your concrete problem.

You can create a file constants.properties

executable.run=C:\\server\\rd.exe
incoming.file=C:\\file.lic
executable.params=-z
log.file=C:\\File\\log.txt

and then after the init you do calls to getStringValue:

String executable = getStringValue("executable.run");
String filein = getStringValue("incoming.file");
String params = getStringValue("executable.params");
String log = getStringValue("log.file");

Then you can do the rt.exec and instead of using hardcoded strings, you can use the ones you retrieve before.