简体   繁体   English

如何从外壳程序脚本以String格式将文件的内容作为参数传递给java方法?

[英]How to pass the contents of the file in String format as a parameter to a java method from shell script?

I am calling a java method from shell script.It accepts String parameter. 我正在从外壳程序脚本中调用Java方法,它接受String参数。 I am supposed to pass whole file content as a parameter. 我应该将整个文件的内容作为参数传递。

Now from shell script I am getting the whole file Content using 现在从shell脚本中,我正在使用获取整个文件的内容

orgFileContent=$(awk '$1=$1' ORS='\\\\n' $orgFile)

when I try to print this in sh..I am getting the whole content.But When I try to print this the called java method.I am just getting first two lines. 当我尝试在sh中打印此文件时。我正在获取全部内容。但是当我尝试打印此被称为java方法的文件时,我仅获得了前两行。

What will be the reason? 会是什么原因呢? How can I pass the whole file as a string? 如何将整个文件作为字符串传递?

Thanks in advance 提前致谢

Suppose you have a static main in class Main. 假设您在Main类中有一个静态main。 You could use xargs command to pass the contents of the file to the executing Java program, ie 您可以使用xargs命令将文件的内容传递给执行中的Java程序,即

$ xargs java -classpath CLASSPATH Main < youInputFile

By doing so, the contents of the file will be passed over the pipe to xargs command hence you could print the file contents with a simple 这样,文件的内容将通过管道传递给xargs命令,因此您可以使用简单的方法打印文件内容

System.out.println(args[0])

However, if you try to execute the command as shown below, any newline character present in your input file will mean a new passed argument, hence the same Java code would print only the first line and then ignore the rest of the file (as the 1st word is 1st arg, ...) 但是,如果您尝试执行如下所示的命令,则输入文件中存在的任何换行符都将意味着传递了新的参数,因此相同的Java代码将仅打印第一行,然后忽略文件的其余部分(因为第一个字是第一个arg,...)

$ cat yourInputFile | xargs java -classpath CLASSPATH Main

只需用双引号将变量

java -classpath CLASSPATH Main "$orgFileContent"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM