我正在通过bash脚本运行一个Java程序. 我的计划是迭代for循环以创建要传递的参数:

lastLine=$(cat $info_file | wc -l)

countID=0

cat $info_file | while read fileID; do

initVar="${initVar} ${fileID}.txt"

countID=$(($countID + 1))

if [ "$countID" == "$lastLine" ]; then
echo $initVar > tmp_${SampleID}.txt
fi

done

InputListVar=$(cat tmp_${SampleID}.txt)

java ProgramX --input "${InputListVar[@]}" --output $output_file

但是,当我运行上面的脚本时,我收到以下消息:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in path at index 15 

我认为这是由于我在InputListVar数组中提供的文件列表中的空间(如这里建议的Illegal character in path at index 16).我怎么才能解决这个问题呢?

推荐答案

我不与java打交道,但我想java会希望看到这样的情况:

java --input file1 file2 file3 --output ...

OP的脚本应该生成这样的格式:IF InputListVar被定义为数组,但它不是.下面创建一个标量变量:

InputListVar=$(cat tmp_${SampleID}.txt)

如果我们假设输入文件的以下内容:

$ cat tmp_${SampleID}.txt
file1
file2
file3

然后,我们得到以下内容:

$ InputListVar=$(cat tmp_${SampleID}.txt)
$ typeset -p InputListVar
declare -- InputListVar=$'file1\nfile2\nfile3'

因此,当它被馈送到java时,会发生以下情况:

java --input "${InputListVar[@]}" --output ...

# becomes

java -- input file1
file2
file3 -- ouput ...

# hence an error

NOTE: ${InputListVar}在技术上与${InputListVar[0]}相同,因此OP的${InputListVar[@]}变为${InputListVar[0]},变为${InputList}

我们可以通过确保使用的是一个数组来"修复"这个问题:

InputListVar=( $(cat tmp_${SampleID}.txt) )           # note the additional pair of outer parens

回到java电话:

java --input "${InputListVar[@]}" --output ...

# becomes

java -- input file1 file2 file3 -- ouput ...

Java相关问答推荐

Spring Webocket:尽管凭据设置为False,但MLhttpsify和Fetch请求之间的CORS行为存在差异

更新我们的一个文物后出现了严重的符号引用错误

是否需要关闭Executors返回的执行器.newVirtualThreadPerTaskExecutor()?

为什么Java中的两个日期有差异?

同时运行JUnit测试和Selenium/Cucumber测试时出现问题

无法传递消费者<;>;实例

确定Java中Math.Ranb()输出的上限

如何判断一个矩阵是否为有框矩阵?

RichFaces 3.x-Spring Boot-迁移web.xml

如何从错误通道回复网关,使其不会挂起

Java ArrayList的整数和数组的泛型

在添加AdMob时无法为Google Play构建应用程序包:JVM垃圾收集器崩溃和JVM内存耗尽

S数学.exp的相同错误保证也适用于StrictMath.exp吗?

在使用具有不同成本的谓词调用allMatch之前对Java流进行排序会带来什么好处吗?

TinyDB问题,无法解析符号';上下文&

为什么我的登录终结点不能被任何请求访问?

如何在Spring Boot中为不同的部署环境管理多个.properties文件?

始终使用Spring Boot连接mongodb上的测试数据库

放置在变量中的Java成员引用不相等

OpenAPI Maven插件生成错误的Java接口名称