You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中使用BeanShell读取非JMX目录的相对文件路径?

How to Reference a Custom File Path in JMeter

Got it, let's fix that path reference for your upload.xml file. The original code uses the test plan's base directory (where your JMX is stored), but since you want to point to c:/project/configfiles/upload.xml instead, here are a few straightforward ways to adjust this:

Option 1: Hardcode the Absolute Path (Simplest)

You can replace the dynamic base directory logic with the full fixed path directly. Instead of the concatenated BeanShell functions, use this single expression:

${__BeanShell("c:/project/configfiles/upload.xml",)}

Or if the field you're using this in accepts plain text (like a file path input), you can even skip the BeanShell function entirely and just enter:

c:/project/configfiles/upload.xml

This is quick and easy, though it means you'll have to edit the path directly in the JMX if it ever changes.

Option 2: Use a JMeter Property (More Flexible)

If you want to avoid hardcoding the path (so you can change it without editing the test plan), define a custom property and reference it:

  1. First, set the property. You can do this in a few ways:

    • Add it to your user.properties file (found in JMeter's bin folder): config.files.dir=c:/project/configfiles
    • Or pass it as a system property when starting JMeter: -Jconfig.files.dir=c:/project/configfiles
    • Or add a "User Defined Variables" element in your test plan with a variable named config.files.dir set to c:/project/configfiles
  2. Then reference it in your path expression using BeanShell:

${__BeanShell(props.get("config.files.dir") + File.separator + "upload.xml",)}

This way, if your config directory moves later, you just update the property instead of every instance of the path in your test plan.

BeanShell is a bit dated—JMeter recommends using JSR223 functions/elements for better performance. Here's the equivalent using JEXL3 (a lightweight scripting language supported by JMeter):

  • Hardcoded version:
${__jexl3("c:/project/configfiles/upload.xml",)}
  • Property-based version:
${__jexl3(props.get("config.files.dir") + File.separator + "upload.xml",)}

All these options will correctly point to your upload.xml file in the c:/project/configfiles/ directory, regardless of where your JMX is stored.

内容的提问来源于stack exchange,提问作者Chaitanya Revalla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:16:51