How to use terminal arguments with values in Java?

前端 未结 4 1684
长发绾君心
长发绾君心 2021-01-25 06:36

For my school project I am creating a game like Bad Apples for iPhone (not my personal choice but it isn\'t the problem).

The game needs to have two versions, the first

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 07:16

    There is an interesting discussion of JavaFX and command line parameters in this blog post: Exploring JavaFX 2 - Accessing application parameters, in which the author recommends that you "let Application.getParameters() to act a transporter and ask Apache Commons CLI to take the main work" by which he means just get the raw parameter data rather than the parsed parameters:

    getParameters().getRaw().toArray(new String[getParameters().getRaw().size()])
    

    This translates the parameters to a JavaFX example into the same kind of String array you get from a command line app, and you can then handle the parameter parsing using a common function.

    If you can't use Apache Commons CLI in your project, you could implement a basic parameter parser for your app yourself which handles in a common function the parsing based upon the parameters retrieved from a getParameters().getRaw().toArray call (in JavaFX mode) or the main() args passed to your app (in command line mode).

提交回复
热议问题