如何在JMeter中使用BeanShell读取非JMX目录的相对文件路径?
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:
First, set the property. You can do this in a few ways:
- Add it to your
user.propertiesfile (found in JMeter'sbinfolder):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.dirset toc:/project/configfiles
- Add it to your
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.
Option 3: Use JSR223 Instead of BeanShell (Recommended for JMeter 3.1+)
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

