Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans how to set command line arguments in Java

I am trying to set command line arguments in a Netbeans 7.1 Java project on Windows 7 64 bit.

Netbeans is not passing the arguments I give it.

I go to Project --> Properties --> Run --> and type the arguments next to "Arguments" however the arguments are not passed to the program. How do I pass them?

like image 843
José Avatar asked Feb 06 '12 23:02

José


People also ask

How do you pass a String as a command-line argument in Java?

Example: Numeric Command-Line Argumentsint argument = Intege. parseInt(str); Here, the parseInt() method of the Integer class converts the string argument into an integer. Similarly, we can use the parseDouble() and parseFloat() method to convert the string into double and float respectively.


Video Answer


2 Answers

  1. Create the Java code that can receive an argument as a command line argument.

    class TestCode{     public static void main(String args[]){         System.out.println("first argument is: "+args[0]);     } } 
  2. Run the program without arguments (press F6).

  3. In the Output window, at the bottom, click the double yellow arrow (or the yellow button) to open a Run dialog.

  4. If the argument you need to pass is testArgument, then here in this window pass the argument as application.args=testArgument.

This will give the output as follows in the same Output window:

first argument is: testArgument 

For Maven, the instructions are similar, but change the exec.args property instead:

exec.args=-classpath %classpath package.ClassName PARAM1 PARAM2 PARAM3 

Note: Use single quotes for string parameters that contain spaces.

like image 36
user2115420 Avatar answered Sep 21 '22 23:09

user2115420


I am guessing that you are running the file using Run | Run File (or shift-F6) rather than Run | Run Main Project. The NetBeans 7.1 help file (F1 is your friend!) states for the Arguments parameter:

Add arguments to pass to the main class during application execution. Note that arguments cannot be passed to individual files.

I verified this with a little snippet of code:

public class Junk {     public static void main(String[] args)     {         for (String s : args)             System.out.println("arg -> " + s);     } } 

I set Run -> Arguments to x y z. When I ran the file by itself I got no output. When I ran the project the output was:

arg -> x arg -> y arg -> z 
like image 96
Paul Avatar answered Sep 20 '22 23:09

Paul