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
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.
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
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
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.
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"
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.