2010-05-24 31 views
4

目前,我有一个.properties文件来存储与框架相关的设置。 实施例:从Ant目标将变量传递给Java方法

default.auth.url=http://someserver-at008:8080/ 
default.screenshots=false 
default.dumpHTML=false 

我已经写了一个类,以提取这些值,这里是该类的方法。

public static String getResourceAsStream(String defaultProp) { 
    String defaultPropValue = null; 
    //String keys = null; 
    try { 
     InputStream inputStream = SeleniumDefaultProperties.class.getClassLoader().getResourceAsStream(PROP_FILE); 
     Properties properties = new Properties(); 
     //load the input stream using properties. 
     properties.load(inputStream); 
     defaultPropValue = properties.getProperty(defaultProp); 

    }catch (IOException e) { 
     log.error("Something wrong with .properties file, check the location.", e); 
    } 
    return defaultPropValue; 
} 

纵观我用的方法,如应用程序下面,只是确切需要的属性:

public String getBrowserDefaultCommand() { 
    String bcmd = SeleniumDefaultProperties.getResourceAsStream("default.browser.command"); 
    if(bcmd.equals("")) 
     handleMissingConfigProperties(SeleniumDefaultProperties.getResourceAsStream("default.browser.command")); 
    return bcmd; 
} 

但我还没有决定做一个改变这一点,并使用Ant和传递参数,而不是使用它来自.properties文件。

我想知道如何将值传递给使用Ant的Java方法。这些类都没有主要的方法,并且不会有任何主要的方法。由于这个,我无法使用java系统属性。

回答

1

我想你会想要在调用java时使用-Dpropname=propvalue语法在命令行上传递属性值。见here

+0

我发现这在提交问题之前进行Google搜索期间的网站,但它不起作用。 – 2010-05-24 21:02:04

0

如果我理解你的问题的权利。

我想用args会比较简单一些。每当你运行一个Java程序

java myJavaProgram [param1, param1, ...] 

正如你所看到的,我们可以在启动java程序时传递多个参数。所有参数将被存储到参数中。

下面是演示如何在java中访问CLI的程序。

public static void main(String[] args){ 
    for(String arg : args){ 
     System.out.println(arg); 
    } 

} 

希望这会有所帮助。

0

使用Ant像下面

<property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/> 
<property name="file" location="ant/docs/manual/index.html"/> 

<exec executable="${browser}" spawn="true"> 
    <arg value="${file}"/> 
</exec> 

你可以从这个与运行参数直接调用类的,或者你可以进行批量与参数已传递给类,然后调用批处理