Netbeans how to set command line arguments in Java

前端 未结 10 1277
陌清茗
陌清茗 2020-11-27 16:36

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

相关标签:
10条回答
  • 2020-11-27 16:52
    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.

    0 讨论(0)
  • 2020-11-27 16:57

    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

    0 讨论(0)
  • 2020-11-27 16:57

    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
    
    0 讨论(0)
  • 2020-11-27 17:02
    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.

    0 讨论(0)
  • 2020-11-27 17:02

    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" 
    
    0 讨论(0)
  • 2020-11-27 17:05

    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.

    0 讨论(0)
提交回复
热议问题