Reputation: 1565
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?
Upvotes: 64
Views: 172182
Reputation: 81
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
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
Step 4: Run the Program by using F6.
Upvotes: 8
Reputation: 1083
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"
Upvotes: 0
Reputation: 1422
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
Upvotes: 1
Reputation: 241
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]);
}
}
Run the program without arguments (press F6).
In the Output window, at the bottom, click the double yellow arrow (or the yellow button) to open a Run dialog.
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.
Upvotes: 24
Reputation: 19
For passing arguments to Run Project command either you have to set the arguments in the Project properties Run panel
Upvotes: 1
Reputation: 101
For a Maven project using NetBeans 8.x:
An example name/value pair might resemble:
javax.persistence.jdbc.password=PASSWORD
Then run your project:
main(...)
.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.
Upvotes: 10
Reputation: 13967
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.
Upvotes: 14
Reputation: 2224
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
Upvotes: 2
Reputation: 41
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
Upvotes: 0
Reputation: 20061
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
Upvotes: 55