Tuesday, February 17, 2009

Using Command Line Arguments in NetBeans IDE

This post covers the following points:
  • How to open the project properties dialog box for NetBeans IDE projects?
  • How to use command line arguments while running programs through NetBeans IDE?
NetBeans IDE provides a way to invoke our Java program having a main method with command line arguments.  Let us walk through this process.  We will create a new Java Application project and will add some code to the Main method which will print the provided command line arguments on the console.

Test Code we have written in the Main method:



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tusharvjoshi.argsdemo;

/**
*
* @author Tushar Joshi
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if (args.length == 0) {

System.out.println("No Command Line arguments");

} else {

System.out.println("You provided " + args.length
+ " arguments");

for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]: "
+ args[i]);
}
}
}
}


Opening the project properties dialog boxWe can either right click on the project name in the Projects panel and choose the properties option, or we can choose the Project Properties option from the File menu.


Saving the command line arguments

In the project Properties dialog box we will have to choose the Run node from the left Categories tree.  As soon as we select the Run node we can see the options for the run configuration.

In the Arguments text box we have to type our command line arguments.  We can see we have typed "one two three" as the options in the dialog box for testing.


Now when we Run the project the project will run as if these command line arguments are being typed in front of the java command.  We can see the output window to check how the arguments are printed by our test code.

with regards
Tushar Joshi, Nagpur

UPDATE: 21 Mar 2014:
There is a plugin available for NetBeans IDE 8.0 which allows interactive command line arguments facility to the IDE, more details are in this post


52 comments:

  1. I've typed this in exactly but debugging the app does not pass any of the command-line options into args[]. What is the problem?

    ReplyDelete
  2. You may have had the same problem that I had...

    The path to your run-time command line args is stored in the netbeans private.properties file
    as "application.args=".

    You may have overridden your private.properties file with an entry in your build.xml of

    target name="-init-private" depends="-pre-init"

    If you have, then args[] won't get picked up at runtime unless you create an overriding entry in your project.properties file to replace the one that should be picked up from your private.properties.

    Hope this might help

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I have tried everything above and nothing works.
    The code above returned "No Command Line Arguments". Anyone have any ideas?

    ReplyDelete
    Replies
    1. do the configuration and try i had same problem but after doing the configuration it works

      Delete
  5. hey its working...
    thank u very much

    ReplyDelete
  6. thanks, it's working

    ReplyDelete
  7. Even if the topic of this post is very trivial, I can see that some people got benefited by reading it.

    Thanks all for the good words.

    with regards
    Tushar Joshi

    ReplyDelete
  8. hi. I am not able to pass the command line arguments to my app? what could be the problem????

    ReplyDelete
  9. Hi, i essentially want to do the same thing, but instead of just words like one, two, and three. I want to pass a text file, thats inside the same package. Can anyone help?
    Thanks

    ReplyDelete
  10. You can very well pass the path and name of the file as a command line parameter and use that file in your program.

    If you keep your file in the same package then the getResource method in the class loader can be used.

    with regards
    Tushar

    ReplyDelete
  11. if you do not have args picked up properly even though you formate it from project's run arguments, you can try to change "run" target in build-impl.xml to say

    ReplyDelete
  12. how can i pass command line arguments when i have too many main in my package?

    ReplyDelete
  13. @Shresth,

    In the same doalog box where you mention command line arguments you will see Main Class as the input box, you will have to type the main class which you want to run.

    I believe you already know that we can execute only ONE main class at a time through one Java command.

    with regards
    Tushar Joshi, Nagpur

    ReplyDelete
  14. its really useful..thanks a lot

    ReplyDelete
  15. @malor: I am facing the same issue mentioned by you.
    I am able to add the arguments through the interface project properties option but it doesn't get picked up. When I check the argument in netbeans private.properties file it comes as
    "application.args= -3.0 2.0" but nothing happens. It still gives my java.lang.ArrayIndexOutOfBoundsException

    I didn't understand how to override the entry in build.xml to get everything back to normal.
    please help me out if any one has any idea.
    Thanks

    ReplyDelete
  16. ThankYOU! ThankYOU! ThankYOU! ThankYOU! ThankYOU! ThankYOU! GOD BLESS YOU !

    ReplyDelete
  17. Not working... N i also checked project.properties file.. I havent overridden application.args
    Plz help..

    ReplyDelete
  18. @Gunit,

    If you provide more details like:
    1) Which version of NetBeans IDE are you using?
    2) What is not working? Is there any error message you are getting? Can you share that message here
    3) What is your application scenerio? Are you following the exact code given in this article or something else?

    I may help you if these details are known.

    with regards
    Tushar Joshi, Nagpur

    ReplyDelete
  19. Thanks... The post is quiet explanatory and easy.

    ReplyDelete
  20. I'm trying to send file path Through main method
    and I did exactly like what you did
    but it still not working
    it gives me this error message
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Security.main(Security.java:277)
    Java Result: 1
    what can I do to solve this problem ?
    Thank You

    ReplyDelete
  21. i cant find label "arguments" and its "textbox" where i can put the values.. how can this happen?

    ReplyDelete
  22. Ok. The post is completely correct. :)

    ReplyDelete
  23. When I input text files into the arguments field, I get FileNotFoundException yet I have the text files saved into the same package! I'm stumped, can someone help me!

    ReplyDelete
  24. Hi Tushar,

    Thanks for the upload showing how to pass the command line argument in Netbeans.

    I did the same thing but still no luck. however i am able to run my program on command prompt but not on NetBeans(Product Version: NetBeans IDE 7.1.2 (Build 201204101705). Any Suggestions please?

    ReplyDelete
  25. hey its working thank you tushar

    ReplyDelete
  26. do as stated .
    Then run the main project(not class) OR press f6 .

    ReplyDelete
  27. Thanks Tushar. Well explained, very useful for newbies like us.

    ReplyDelete
  28. Thanks a lot, worked for me when I click on the visual button to run the main method, but When I use Shift-F6 it doesnt, hope it will be hepful for some folks :)

    ReplyDelete
  29. thanks a lot, it's working thank you very much

    ReplyDelete
  30. Thank you very much!!! I was having so many problems with this!!

    ReplyDelete
  31. Thank you so much! Best wishes for you!

    ReplyDelete
  32. The Answer is easy in fact :
    1-right click in the name of project like "myFirstProject".
    2-choose ------> properties.
    3-choose ------>Run.
    4-in the argument text field put your args like----------
    one two three
    here we put 3 argument

    5- every body do the same above but the magic touch is ------> right click in the "name of project" which is in our case "myFirstProject"
    and choose --------> Run
    like that the args will work,
    but if you gone to main class and choose right click then choose ---> Run the args will not work.

    ReplyDelete
    Replies
    1. If you want to run any file with arguments, you may like to take a look at the plugin RunWithArgs mentioned in this post http://netbeanside61.blogspot.in/2014/03/command-line-arguments-for-java.html

      Delete
    2. thank you Tushear , but the plug in in the above url just work with JDK 8 only

      Delete
    3. Not JDK8.0 this plugin works with NetBeans IDE 8.0 which is the latest version and everyone who can must shift to the latest version of NetBeans IDE. It also runs on Java 7.0

      Delete
    4. Tanks a lot, it works.
      Mounir NAJAHI

      Delete
  33. Thanks tushar...thanks alot...

    ReplyDelete