[英]How to get arguments from console in java when using a bash file?
I have a java project that I launch using a bash file : 我有一个使用bash文件启动的Java项目:
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest
I launch my bash in my terminal (in ubuntu) : 我在终端(在ubuntu中)启动bash:
firefrost@firefrost-PC:~/ProjetPOO3A$ bash test.sh banking.Account
In my main, I try to get arguments as : 在我的主体中,我尝试获取参数为:
public class RunTest {
public static void main(String[] args) {
System.out.println(args.length);
for(int i = 0; i < args.length; i++)
{
...
The problem is that args.length is 0 when it should be 1 because I passed the argument "banking.Account" in my console. 问题是args.length应该为1时为0,因为我在控制台中传递了参数“ banking.Account”。
If I put the argument in the bash file : 如果我把参数放在bash文件中:
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest banking.Account
my main recognize the argument and the System.out.println outputs 1. 我的主要人知道参数,并且System.out.println输出1。
Why is the argument in the console not taken into account? 为什么不考虑控制台中的参数?
There is no problem in Java code. Java代码没有问题。 Your parameters are not propagated to
java
command in shell script. 您的参数不会在shell脚本中传播到
java
命令。
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest "$@"
The $@
variable contains all parameters passed to shell script. $@
变量包含传递给shell脚本的所有参数。
Command line arguments to a bash script can be accessed with $1
, $2
, etc . bash脚本的命令行参数可以用
$1
, $2
等访问 。 Here's a fuller explanation . 这是更完整的解释 。
Try updating your script as follows: 尝试如下更新脚本:
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest $1
You forget to process the argument in the bash file. 您忘记处理bash文件中的参数。
You can access them by using $1, $2, $3, $... variables. 您可以使用$ 1,$ 2,$ 3,$ ...变量来访问它们。
For example writing ./my.sh test test2
$1 will contain test and $2 test2 例如,编写
./my.sh test test2
$ 1将包含test和$ 2 test2
So change your script to 因此,将您的脚本更改为
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest $1
Add "$@"
to pass the arguments to the program: 添加
"$@"
以将参数传递给程序:
#!/bin/bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest "$@"
Don't forget the quotes around $@
so bash will "protect" each argument: 不要忘记
$@
周围的引号,因此bash将“保护”每个参数:
@
@
Expands to the positional parameters, starting from one.
从一个开始扩展到位置参数。 When the expansion occurs within double quotes, each parameter expands to a separate word.
当在双引号内进行扩展时,每个参数都会扩展为单独的单词。 That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word.
也就是说,“ $ @”等效于“ $ 1”“ $ 2” ...如果在单词中出现双引号扩展名,则第一个参数的扩展名将与原始单词的开头部分连接在一起,并且扩展名最后一个参数中的表示与原始单词的最后一部分结合在一起。 When there are no positional parameters, "$@" and $@ expand to nothing (ie, they are removed).
当没有位置参数时,“ $ @”和$ @扩展为空(即,它们被删除)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.