0

In java, you can pass cmd line arguments to the main() method during program startup.

I'd like to pass cmd line arguments to a specific method that I call from a script directly. I'd had no luck just entering them as I would for main().

Example:

public class Test {
    public static void main(String[] args) { // args is cmd line input
        // do stuff with args
    }
}

You would do: ~]# java Test cmdlineinput

I need to do:

public class Test {
    public void someMethod(String input) {
        // do stuff with input
    }
}

I want to do: ~]# java Test.someMethod cmdlineinput

Is this possible and how so?

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97

2 Answers2

5

Your program needs a main method to run, and you have to pass the command line arguments directly or indirectly from the main method to wherever you desire it to go. I know of no other solution.

i.e.,

public static void main(String[] args)  {
  Test test = new Test();
  test.someMethod(args);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I think he wants to call a method directly on the Class from the command-line, which is odd but that's what he is saying – Engineer2021 Feb 10 '14 at 20:39
  • @Brian: I think you're right, but as I state in my answer, afaik, this can't be done. It sure as heck would be nice to have some context though, you know? – Hovercraft Full Of Eels Feb 10 '14 at 20:42
  • 1
    https://stackoverflow.com/a/13338025/2591612 – Engineer2021 Feb 10 '14 at 20:43
  • 1
    @Brian: I'll be damned. Why don't you embellish on this and make it an answer? – Hovercraft Full Of Eels Feb 10 '14 at 20:47
  • CLI parsing inside of business code smells. – Tim Bender Feb 10 '14 at 20:54
  • 1
    @TimBender: I've not heard of this code smell. Do you have a link for this so I can learn more? – Hovercraft Full Of Eels Feb 10 '14 at 20:57
  • 1
    http://en.wikipedia.org/wiki/Code_smell http://misko.hevery.com/code-reviewers-guide/ http://en.wikipedia.org/wiki/Reusability The question, is that `String` argument the real parameter to the method? Or will it be parsed into the proper inputs? Would I have to `toString` the real parameters to use that bit of code elsewhere in a program? – Tim Bender Feb 10 '14 at 23:13
  • @TimBender What? If you're using Windows, Linux/Unix, or Mac OSX, half of the programs you use on a daily basis take cmd line arguments, they are just abstracted via UI of some sort. – SnakeDoc Feb 10 '14 at 23:28
  • @TimBender also, as far as I know, String is the only data type that can be passed into a program via the cmd line. It would be up to the program to attempt data conversion if necessary (ie, parse an Integer out of String, ensuring it's a number, etc). – SnakeDoc Feb 10 '14 at 23:31
  • @TimBender: None of those links states that you can't use the arguments as shown. – Engineer2021 Feb 11 '14 at 12:46
  • 1
    @GIJoe: Correct me if I'm wrong, but I think perhaps his argument is that using naked Strings as surrogates for what should be non-String data, is not clean. – Hovercraft Full Of Eels Feb 11 '14 at 13:25
  • 1
    @SnakeDoc lol. I was referencing the idea of using a REPL to call `someMethod` directly. This answer should be the accepted answer. My guess is that the OP has presented an "XY Problem" and `main` would suffice if they knew how exec java in a script. – Tim Bender Feb 11 '14 at 20:00
  • 1
    @HovercraftFullOfEels Precisely! The point of `main` (imo) is to be that layer which parses the String inputs and sets up the rest of the application to run. In addition to the obvious of providing a standardized entry-point for the JVM. – Tim Bender Feb 11 '14 at 20:03
2

By convention, the JVM will look for main and call that. You can't call methods directly from the command-line. However, if you use a REPL for the JVM such as Groovy and groovysh you can call methods from within the shell. This may or may not work for your use case. However, it would technically allow you to individually call Java methods and invoke them. Read more at this question.

Engineer2021
  • 3,288
  • 6
  • 29
  • 51