2013-10-08 91 views
2

我已经阅读了很多关于避免java中硬编码的文章。但无法弄清楚如何将其应用于我的要求。在做了一些研究之后,我会问这个问题。以下是我的代码片段。因为我想避免Process pr = rt.exec()中的路径名的硬编码。关于如何执行它的任何建议。提前感谢。如何避免Java中的硬编码

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:\文件\ 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; 

} 
} 
+0

什么需要在什么情况下 –

回答

4

你必须severals事情尝试:

Java属性

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); 
} 

和属性文件将类似于

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

但你也可以使用Spring上下文

<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> 

和内部database.properties的元素将被访问的方式

"${jdbc.username}" 

-

为了您的具体问题。

您可以创建一个文件constants.properties

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

,然后你调用向GetStringValue初始化后:

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

然后,你可以做和的rt.exec而不是使用硬编码字符串,你可以使用之前检索到的。

+0

@戴夫牛顿改变:?感谢您的快速reply.My实际需要,如果是你注意到我的问题Process pr = rt.exec(“C:\\ server \\ rd.exe -a C:\\ file.lic -z [+] C:\\ File \\ log.txt”) ;我开始一个可执行文件rd。exe文件,该文件位于C:\中,并将这些详细信息存储在位于C:\ File中的日志文件中。 所有这些我想避免硬编码。 – user2821894

+0

编辑您的理解 – RamonBoza

+0

谢谢,我认为这是行得通的。但我如何在rt.exec – user2821894

3
  • 使用的系统属性,例如,-Dpath=...System.getProperty("path");
  • 使用配置文件
  • 通行证它在命令行上
  • 从JNDI得到它

换句话说,有很多不同的方式,最好的取决于你的实际需要。

0

您也可以将该字符串分配给名为private final static String的字符串。这样的代码变得更具可读性(而且更容易,如果你需要在以后更改更新

private final static String rtCommand = ""C:\\server\\rd.exe -a C:\\file.lic -z[+] C:\\File\\log.txt";//or something more smartly named. You would know what to name it. 
.... 
Process pr = rt.exec(rtCommand); 
+0

是的我已经尝试过这种方式,但它确实承认字符串。可能是因为我在一个String中有多个路径。 – user2821894

+0

@ user2821894很奇怪,它应该是一样的。我认为你做错了什么,也许是一个错字。 –