运行具有更新属性值的批处理文件

时间:2014-05-28 16:11:21

标签: java windows batch-file properties

我完全迷失了如何做到这一点......所以一些帮助会很好。所以我用我的java程序创建了一个批处理文件(使用查询来收集一些数据并将其作为CSV文件输出)。我还有一个属性文件,以便我可以更改查询的参数。

我的批处理文件运行并执行我的代码,但问题是,当我更新我的属性文件中的查询参数以获得不同的结果时,我的批处理文件不会使用更新的值运行,而只运行旧的。

title sender
echo@ off
java -jar "googleanalyticsfile.jar"

这是我在我的蝙蝠文件中所拥有的,任何帮助都是很好的thx

编辑:

所以这是我在java文件中的查询

private static GaData executeDataQuery(Analytics analytics, String metrics, String dimensions, String startDate, String endDate, String sort, String profileID) throws IOException {
    return analytics.data().ga().get("ga:" + profileID, // Table Id. ga: + profile id.
        startDate, // Start date.
        endDate, // End date.
        metrics) // Metrics.
        .setDimensions(dimensions)
        .setSort(sort)
        .setMaxResults(100)//this value needs to be an int not string
        .execute();

这是我初始化查询参数的地方 public static void main(String [] args){

 try {
      ...some code
      //stuff imported from properties file
      CrunchifyGetPropertyValues metricValues = new CrunchifyGetPropertyValues();
      String metrics = metricValues.metrics();
      String dimensions = metricValues.dimensions();
      String startDate = metricValues.startDate();
      String endDate = metricValues.endDate();
      String sort = metricValues.sort();
      String profileID = metricValues.profileID();
      if (profileID == null) {
        System.err.println("No profiles found.");
      } else {
        GaData gaData = executeDataQuery(analytics, metrics, dimensions, startDate, endDate, sort, profileID);
        printGaData(gaData);
      }

编辑:

public class CrunchifyGetPropertyValues {

        public String metrics() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String Metrics = prop.getProperty("Metrics");

            result = Metrics;       
            System.out.println(result);
            return result;
        }
        public String dimensions() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String Dimensions = prop.getProperty("Dimensions");

            result = Dimensions;       
            System.out.println(result);
            return result;
        }
        public String startDate() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String startDate = prop.getProperty("startDate");

            result = startDate;       
            System.out.println(result);
            return result;
        }
        public String endDate() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String endDate = prop.getProperty("endDate");

            result = endDate;       
            System.out.println(result);
            return result;
        }
        public String sort() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String sort = prop.getProperty("sort");

            result = sort;       
            System.out.println(result);
            return result;
        }
        public String maxResults() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String maxResults = prop.getProperty("maxResults");

            result = maxResults;       
            System.out.println(result);
            return result;
        }
        public String profileID() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String profileID = prop.getProperty("profileID");

            result = profileID;       
            System.out.println(result);
            return result;
        }
    }

    and i call the properties file by each string does that help at all?

    EDIT:

    public class CrunchifyGetPropertyValues {

        public String metrics() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String Metrics = prop.getProperty("Metrics");

            result = Metrics;       
            System.out.println(result);
            return result;
        }
        public String dimensions() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String Dimensions = prop.getProperty("Dimensions");

            result = Dimensions;       
            System.out.println(result);
            return result;
        }
        public String startDate() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String startDate = prop.getProperty("startDate");

            result = startDate;       
            System.out.println(result);
            return result;
        }
        public String endDate() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String endDate = prop.getProperty("endDate");

            result = endDate;       
            System.out.println(result);
            return result;
        }
        public String sort() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String sort = prop.getProperty("sort");

            result = sort;       
            System.out.println(result);
            return result;
        }
        public String maxResults() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String maxResults = prop.getProperty("maxResults");

            result = maxResults;       
            System.out.println(result);
            return result;
        }
        public String profileID() throws IOException {

            String result = "";
            Properties prop = new Properties();
            String propFileName = "config.properties";

            InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
            prop.load(inputStream);
            if (inputStream == null) {
                throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
            }

            // get the property value and print it out
            String profileID = prop.getProperty("profileID");

            result = profileID;       
            System.out.println(result);
            return result;
        }
    }

是的......很多代码可能会被浓缩,但那就是我的CrunchifyGetPropertyValues

1 个答案:

答案 0 :(得分:0)

这是一支吸烟枪:

InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

您正在jar文件中加载属性文件。

磁盘上的文件永远不会被考虑。

您必须提供备用属性文件的完整路径,作为main方法的参数,或作为系统属性。

顺便说一句,您最好在inputStream致电之前检查null是否不是prop.load