eclipsedrjava

Pass arguments to program in Eclipse QUICKLY


I'm learning Java and I've been trying to get used to Eclipse. It's slick and I like it, but it is annoying to change input arguments for the programs I'm writing. I know I have to go to "Run configurations" etc and enter the arguments under the arguments tab, but to do so every time is a pain in neck.

In Dr. Java, my old IDE, there was a console docked to the bottom of the editor where one could enter arguments directly into the command line, along the lines of

" >Run myClass some_argument "

Is there some similar setting in Eclipse that makes it possible to run a program with arguments without having to go through the "run configurations" menu every time?


Solution

  • In eclipse it is possible to create Run configurations per project & adding arguments, like you've been doing so far. After you've created your configuration you can easily find it by clicking the down arrow next to the Run button. Here you can select the configuration to run, and it will run it with the arguments you've provided in that configuration.

    You could also do something like this, not sure if this is the best way to do it it's just something I thought of:

    public static void main(String[] args) {
        if (args.length == 0) {
            // If no arguments are provided run it with these ones
            myCustomMain(new String[] { "argument" });
        } else {
            // If arguments are provided run it with those arguments
            myCustomMain(args);
        }
    }
    
    public static void myCustomMain(String[] args) {
        // Do stuff with the arguments
    }
    

    You could even go as far as using the Scanner class to write code which will ask you for the arguments when you run your program