Search code examples
javaenvironment-variablesprocessbuilder

Setting null terminated string in environment variables via ProcessBuilder


As the caption states I'm trying to set an environment variable with multiple null terminated filenames.

My code looks like this:

ProcessBuilder pb = new ProcessBuilder(execCmd);
Map<String, String> env = pb.environment();
env.clear();
String storedFiles = "";
Iterator<Attributes> storedSequence = info.getSequence().iterator();
while (storedSequence.hasNext()) {
    storedFiles += storedSOPSequence.next().getFilename() + "\0";
}
env.put("StoredFiles", storedFiles);

try {
    pb.start();
} catch (Exception e) {

}

But I run into the following excepion:

Exception in thread "pool-1-thread-2" java.lang.IllegalArgumentException:
Invalid environment variable value: "/tmp/tmp.DXrJMdJmbW/53cd50f9"

Is there a way to achieve this?


Solution

  • You should not use nulls as separators in strings, especially when passing the strings to the operating system.

    The O/S interprets null as an end-of-string marker and may not copy anything after that.

    Use another control character instead (e.g. '\r').