1

I have a java project that I launch using a bash file :

#!/usr/bin/env bash

java -classpath logger/bin:banking/bin:testframework/bin test.RunTest

I launch my bash in my terminal (in ubuntu) :

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.

If I put the argument in the bash file :

#!/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.

Why is the argument in the console not taken into account?

2

4 Answers 4

2

There is no problem in Java code. Your parameters are not propagated to java command in shell script.

#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest "$@"

The $@ variable contains all parameters passed to shell script.

Sign up to request clarification or add additional context in comments.

Comments

1

Command line arguments to a bash script can be accessed with $1, $2, etc. 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

2 Comments

Thanks! does $@ gets all arguments I pass?
That's a good question. The answer is to use $@. Someone else has (thoughtfully) already covered that option. It's also covered in the link I posted.
1

You forget to process the argument in the bash file.

You can access them by using $1, $2, $3, $... variables.

For example writing ./my.sh test test2 $1 will contain test and $2 test2

So change your script to

#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest $1

Comments

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:

@

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. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

See bash man.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.