62

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
José
  • 1,525
  • 3
  • 14
  • 12
  • Please show your main class. Are you using a NetBeans-created build.xml or your own build.xml? – Paul Feb 06 '12 at 23:15
  • Are you sure you are editing the run configuration that is used when you run the project? Make sure you select the correct configuration in the drop down of the toolbar –  Feb 06 '12 at 23:19
  • If you want it to work for a single file.. call the main in some other class `MainProgram.main(new String[]{"arg0","arg1"});` – Sorter Nov 29 '13 at 06:15

10 Answers10

54

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
Paul
  • 19,704
  • 14
  • 78
  • 96
  • We can pass command line argument even to a single Java file now using a community contributed plugin for NetBeans IDE 8.0 https://github.com/tusharvjoshi/nbrunwithargs – Tushar Joshi Mar 22 '14 at 02:13
  • Oh, yes, this plugin was created for Ant based Java projects only. I will modify it to not show the NPE in Maven projects. This plugin needs additional functionality of Maven projects, the plugin is open for pull requests and any help provided by community for this feature – Tushar Joshi Nov 29 '14 at 02:01
  • If I get a chance I will take a look to see what's involved for making it work with Gradle projects as well, though Gradle itself requires a plugin so not sure if that's possible. – Paul Nov 29 '14 at 16:45
24
  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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user2115420
  • 241
  • 2
  • 2
  • If I insert *application.args=testArgument* after clicking the yellow button, then, once I click Run on that window, I get the following error: *Target "application.args=9" does not exist in the project "{0} (run)"* - I entered a 9 now, but what I enter as an argument isn't relevant for the error... – Martin Jul 22 '18 at 17:53
  • just to know, on my NetBeans 11.3, if the "compile on save" option is disabled, the "application.args" property does not work – Pedro Siqueira Apr 14 '20 at 01:36
13

If it's a Maven project then Netbeans is running your application using the exec-maven-plugin so you'll need to append your options onto the existing exec.args property found in the Run Maven dialog. This dialog can be accessed from the the Output window by pressing the yellow double arrow icon.

enter image description here

Johnny Baloney
  • 3,409
  • 1
  • 33
  • 37
Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
  • 1
    This is the easiest method that I've found for NetBeans Maven projects! The section "Remember as" and other options does not appear for me when that dialog opens. I have to resize the dialog before they become visible. Even after I have used "Remember as 'foo'", I don't know how to re-run that configuration. I don't know where it was "remembered". – Enwired Feb 02 '17 at 00:46
10

For a Maven project using NetBeans 8.x:

  1. Click Run >> Set Project Configuration >> Customise
  2. Select Actions
  3. Select Run file via main()
  4. Set name/value pair to include the arguments.
  5. Click OK

An example name/value pair might resemble:

javax.persistence.jdbc.password=PASSWORD

Then run your project:

  1. Open and focus the Java class that includes main(...).
  2. Press F6 to run the program.

The command line parameters should appear in the Run window.

Note that to obtain the value form with the program, use System.getProperty().

Additional actions for Test file, Run project, and other ways to run the application can have arguments defined. Repeat the steps above for the different actions to accomplish this task.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user2225887
  • 101
  • 1
  • 2
  • What if Set Project Configuration is greyed out?? Grails project. – user2782001 Jan 02 '17 at 01:34
  • Environment variables obtained from `System.getProperty()` are not the same thing as "command-line arguments". If you need to provide the arguments as seen by the array `args` in `main(String[] args)` then it is necessary to edit the Maven variable `exec.args` The answer by "Leif Gruenwoldt" shows one way to do that. – Enwired Feb 02 '17 at 00:40
8
import java.io.*;
class Main
{
public static void main(String args[]) throws IOException
{
    int n1,n2,n3,l;
    n1=Integer.parseInt(args[0]);
    n2=Integer.parseInt(args[1]);
    n3=Integer.parseInt(args[2]);
    
    if(n1>n2)
    {
        l=n1;
    }
    else
    {
        l=n2;
    }
    
    if(l<n3)
    {
        System.out.println("largest no is "+n3);
    }
    else
    {
        System.out.println("largest no is "+l);
    }
    
}}

Consider above program, in this program I want to pass 3 no's from Command Line, to do so.

Step 1 : Right Click on Cup and Saucer icon, u'll see this window

1

Step 2: Click on Properties

Step 3: Click Run _> Arguments _> type three no's eg. 32 98 16 then click OK. Plz add space between two arguments. See here

2

Step 4: Run the Program by using F6.

2

In NetBeans IDE 8.0 you can use a community contributed plugin https://github.com/tusharvjoshi/nbrunwithargs which will allow you to pass arguments while Run Project or Run Single File command.

For passing arguments to Run Project command either you have to set the arguments in the Project properties Run panel, or use the new command available after installing the plugin which says Run with Arguments

For passing command line arguments to a Java file having main method, just right click on the method and choose Run with Arguments command, of this plugin

UPDATE (24 mar 2014) This plugin is now available in NetBeans Plugin Portal that means it can be installed from Plugins dialog box from the available plugins shown from community contributed plugins, in NetBeans IDE 8.0

Run with Arguments plugin as shown in Plugin dialog box

Tushar Joshi
  • 2,206
  • 21
  • 20
  • Does not work for me.. It says cannot found main class – wmfairuz Sep 14 '14 at 18:36
  • Doesn't work for Maven projects - see this [bug report](https://github.com/tusharvjoshi/nbrunwithargs/issues/3) – Johnny Baloney Nov 27 '14 at 14:52
  • 1
    Oh, yes, this plugin was created for Ant based Java projects only. I will modify it to not show the NPE in Maven projects. This plugin needs additional functionality of Maven projects, the plugin is open for pull requests and any help provided by community for this feature – Tushar Joshi Nov 29 '14 at 02:02
1

For passing arguments to Run Project command either you have to set the arguments in the Project properties Run panel

1

Note that from Netbeans 8 there is no Run panel in the project Properties.

To do what you want I simply add the following line (example setting the locale) in my project's properties file:

run.args.extra=--locale fr:FR
jjazzboss
  • 1,261
  • 8
  • 14
0

IF you are using MyEclipse and want to add args before run the program, Then do as follows: 1.0) Run -> Run Config 2.1) Click "Arguments" on the right panel 2.2)add your args in the "Program Args" box, separated by blank

njuhgn
  • 31
  • 3
0

This worked for me, use the VM args in NetBeans:

@Value("${a.b.c:#{abc}}"
...

@Value("${e.f.g:#{efg}}"
...

Netbeans:

-Da.b.c="..." -De.f.g="..."

Properties -> Run -> VM Options -> -De.f.g=efg -Da.b.c=abc

From the commandline

java -jar <yourjar> --Da.b.c="abc" 
Roland Roos
  • 1,003
  • 10
  • 4