0

I have a class that contains the following main method:

public static void main(String[] args) {
    System.out.println("args.length: " + args.length);
    if (args.length == 1) {
        // do something
   } else if (args.length == 2){
       // do something
   }
    ... Some code ...
}

The problem is that the arguments in command line are not read.

When I type ./program arg1 Arg 2 .... I always get args.length equals to 0. I tried to verify the length of arguments in other classes and I got the correct number

What could be the problem ?

3
  • 2
    ./program ? Where does this executable come from ? Commented Nov 5, 2014 at 21:20
  • 7
    You would normally start a Java program with java foo.bar.Baz arg1 arg2 ... - what is ./program? (If it's a shell script you've written to start the application, that's probably where the problem is.) Commented Nov 5, 2014 at 21:20
  • 1
    are you are running the program you think you are running? java program run like this btw: java -cp <...> your.MainClass arg1 arg2 ... Commented Nov 5, 2014 at 21:21

2 Answers 2

3

Assuming program is a script, it should look like this :

#!/bin/sh
java foo.bar.YourClass "$@"

It should now work with arguments

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

Comments

-1

./program is a script. In fact, I forgot to add $* at the end of " java -cp <...> myclass ". So, it was normal not to take into account the command line arguments. Thanks for all of you !

1 Comment

-1 - Use "$@" instead of $* if you want quoted arguments to be handled correctly by the wrapper script.

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.