在Java应用程序中使用命令行参数

Command-line arguments can be a way of specifying configuration properties for an application, and Java is no different. Instead of clicking on an application icon from the operating system, you can run the Java application from a terminal window. Along with the application name, a number of arguments can follow which are then passed to the application's starting point (i.e., the main method, in the case of Java).

命令行参数可以是为应用程序指定配置属性的一种方式,而Java也不例外。 您可以从终端窗口运行Java应用程序,而不必从操作系统上单击应用程序图标。 随同应用程序名称一起,可以跟随多个参数,然后将这些参数传递到应用程序的起点(例如,对于Java,是主方法)。

For example, NetBeans has a number of startup parameters that can be passed to the application when it is run from a terminal window (e.g.,

例如, NetBeans具有许多启动参数,当它们从终端窗口运行时可以传递给应用程序(例如,

specifies a version of the JDK to be used instead of the default JDK associated with the NetBeans application).

指定要使用的JDK版本,而不是与NetBeans应用程序关联的默认JDK。

主要方法 ( The Main Method )

Let's examine the main method to see where the arguments passed to an application appear:

让我们检查main方法,以查看传递给应用程序的参数出现在哪里:

The command-line arguments can be found in the

命令行参数可以在

called

For example, let's consider an application called

例如,让我们考虑一个名为

whose only action is to print out the command-line arguments passed to it:

其唯一的作用是打印出传递给它的命令行参数:


public class C


   public static void main(String[] args) {
//check to see if the String array is empty
if (args.length == 0)
{
System.out.println("There were no commandline arguments pass

       //For each String in the String array
//print out the String.
for(String argument: args)
{
System.out.println(argument);

Syntax of Command Line Arguments

The Java Runtime Engine (JRE) expects arguments to be passed following a particular syntax, like so:

Above, "java" invokes the JRE, which is followed by the name of the program you are calling. These are followed by any arguments to the program. There is no limit to the number of arguments a program can take, but the order is critical. The JRE passes the arguments in the order in which they appear on the command line. For example, consider this code snippet from above:

public class Co

When arguments are passed to a Java program, args[0] is the first element of the array (value1 above), args[1] is the second element (value2), and so on. The code args.length() defines the length of the array.

Passing Command-Line Arguments

In NetBeans, we can pass command-line arguments without having to build the application and run it from a terminal window. To specify the command-line arguments:

  1. Right-click on the project folder in the

    Projects
    window.
  2. Choose the

    Project Properties
    window. 
  3. In the

    Run
  4. In the

    Apple Banana Carrot
    in the
    CommandLineArgs
    program listed above, we will get the output:

Parsing the Command-Line Arguments

Typically, a command line argument is passed with some information about what to do with the value being passed. The argument informing the application what the argument is for typically has a hyphen or two before its name. For example, the NetBeans example for the startup parameter specifying the JDK path is

This means you'll need to parse the command-line arguments to figure out what to do with the values. There are several Java command-line frameworks for parsing command-line arguments. Or you could write a simple command-line parser if the arguments you need to pass are not that many:

The code above either prints the arguments or add them together if they are integers. For example, this command line argument would add the numbers:


       //For each String in the String array
//print out the String.
for(String argument: args)
{
System.out.println(argument);

Syntax of Command Line Arguments

The Java Runtime Engine (JRE) expects arguments to be passed following a particular syntax, like so:

Above, "java" invokes the JRE, which is followed by the name of the program you are calling. These are followed by any arguments to the program. There is no limit to the number of arguments a program can take, but the order is critical. The JRE passes the arguments in the order in which they appear on the command line. For example, consider this code snippet from above:

public class Co

When arguments are passed to a Java program, args[0] is the first element of the array (value1 above), args[1] is the second element (value2), and so on. The code args.length() defines the length of the array.

Passing Command-Line Arguments

In NetBeans, we can pass command-line arguments without having to build the application and run it from a terminal window. To specify the command-line arguments:

  1. Right-click on the project folder in the

    Projects
    window.
  2. Choose the

    Project Properties
    window. 
  3. In the

    Run
  4. In the

    Apple Banana Carrot
    in the
    CommandLineArgs
    program listed above, we will get the output:

Parsing the Command-Line Arguments

Typically, a command line argument is passed with some information about what to do with the value being passed. The argument informing the application what the argument is for typically has a hyphen or two before its name. For example, the NetBeans example for the startup parameter specifying the JDK path is

This means you'll need to parse the command-line arguments to figure out what to do with the values. There are several Java command-line frameworks for parsing command-line arguments. Or you could write a simple command-line parser if the arguments you need to pass are not that many:

The code above either prints the arguments or add them together if they are integers. For example, this command line argument would add the numbers:

翻译自: https://www.thoughtco.com/using-command-line-arguments-2034196

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值