Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse : how we take arguments for main when run

Tags:

java

eclipse

In Java, for a normal main method :

public static void main(String[] args){
    // code here
}

String[] args is used to take some parameters from command line. I can run this file from command prompt by :

javac filename.java
java filename -30

But, it takes more steps, and I must cd to this folder. (to long for each time). So, Does anyway to run this file with some arguments for main in Eclipse.

Thanks :)

like image 583
hqt Avatar asked Aug 31 '12 20:08

hqt


People also ask

Are arguments passed to main During runtime from the command line?

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of String s. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String : "friends.

Can we pass arguments in main () in Java?

You can write the public static void main() method with arguments other than String the program gets compiled. Since the main method is the entry point of the Java program, whenever you execute one the JVM searches for the main method, which is public, static, with return type void, and a String array as an argument.


2 Answers

In Eclipse you can set up a Run Configuration for the Java Application. Click on the green "play" button in the Launch toolbar (next to the bug icon which starts debugging).

Within that configuration, you can set the working directory and command line arguments - and even prompt the user for command line arguments when it's run, using arguments like ${string_prompt:Foo}.

like image 75
Jon Skeet Avatar answered Sep 29 '22 05:09

Jon Skeet


Right click the class. Then go to Run as > Run configurations. Select the program on the left side. Then on the arguments tab you will see Program Arguments. Enter your program arguments in this textarea, if you would like to pass multiple arguments separate the arguments by spaces.

enter image description here

This site provides a good step by step tutorial with images: http://www.javaprogrammingforums.com/java-jdk-ide-tutorials/362-how-send-command-line-arguments-eclipse.html

like image 43
Kevin Bowersox Avatar answered Sep 29 '22 07:09

Kevin Bowersox