简体   繁体   English

使用bash文件时如何从Java中的控制台获取参数?

[英]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). 当没有位置参数时,“ $ @”和$ @扩展为空(即,它们被删除)。

See bash man . bash man

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

相关问题 如何使用bash读取文件的内容,然后拆分该文件中的行,并以它们为参数调用Java程序? - How to read the contents of a file using bash, then split the lines in that file, and call a java program with them as arguments? 使用nohup调用java应用程序时如何从控制台获取密码 - How to get password from console when java application is invoked using nohup 如何使用C#从Java进程获取带有参数的ProcessStartInfo? - How to get ProcessStartInfo with arguments from a java process By using C#? 如何创建 bash 文件以使用 VM 参数启动 Java 程序 - How create bash file to launch Java program with VM Arguments 如何使用Java将文件列表从控制台存储到目录? - How to store file list from console to directory using java? 使用不推荐使用的api时如何从Java控制台过滤警告 - how to filter warnings from console in java when using deprecated api 在编译Java类并从命令控制台运行Java文件时,如何包含Java jar文件? - How will I include a Java jar file when compiling a Java class and running the Java file from the command console? 从 Java 调用 bash 时找不到文件 - File not found when calling bash from Java 如何从控制台将无限 stream (bash) 转换为 java args? - How to make infinite stream (bash) into java args from console? 使用Java控制台程序中的.properties文件 - Using .properties file from Java console program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM