Jmeter java协议配置文件导入

一. 方法一

     通过ClassLoader获取当前路径,想在java脚本里读取druid.properties,路径如下

       apache-jmeter

              --bin

                 --druid.properties

     相关代码:      

 public static void loadProps() {
              String execPath = JdbcWrapper.class.getClassLoader().getClass().getResource("").getPath();
              String realPath = execPath.substring(execPath.indexOf(":")+1, execPath.indexOf("!")-16);
              Properties tempProperties = new Properties();
              InputStream in = null;
              try {
                     in = new BufferedInputStream(new FileInputStream(realPath + "druid.properties"));
                     // in = new BufferedInputStream(new
                     // FileInputStream("system.properties"));
                     tempProperties.load(in);
              } catch (IOException e) {
                     log.error("加载system.properties文件失败", e);
                     throw new RuntimeException("加载system.properties文件失败", e);
              } finally {
                     if (in != null) {
                            try {
                                   in.close();
                            } catch (IOException e) {
                                   log.error("关闭system.properties文件失败", e);
                                   throw new RuntimeException(
                                                 "关闭system.properties文件失败", e);
                            }
                     }
              }
              properties = tempProperties;
       }

二.方法二 

   通过打包配置文件到jmeter执行jar包的的方式

1. 代码里调用方式

      

 in = new BufferedInputStream(new FileInputStream("system.properties"));

2. 将配置文件导入jar包

由于bin目录并不在jmeter的classpath中,所以需要执行一些额外的工作来把jndi.properties添加到jmeter的classpath中,把jndi.properties打包到jmeter的启动jar包中。jmeter的启动jar包为JMETER_HOME/bin/ApacheJMeter.jar,所以需要把jndi.properties 打包到这个 jar 文件中,或者执行如下操作,打开命令行窗口,并定位到 JMETER_HOME/bin 目录,运行如下命令 jar uf ApacheJMeter.jar jndi.properties

原文地址:https://www.cnblogs.com/onmyway20xx/p/4229267.html