java中的命令行参数

Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument.

Java中的命令行参数用于将参数传递给主程序。 如果您查看Java main方法的语法,它将接受String数组作为参数。

When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument. The arguments have to be passed as space-separated values.

当我们传递命令行参数时,它们被视为字符串并传递给字符串数组参数中的main函数。 参数必须以空格分隔的值传递。

We can pass strings and primitive data types as command-line arguments. The arguments will be converted to strings and passed into the main method string array argument.

我们可以将字符串和原始数据类型作为命令行参数传递。 参数将转换为字符串,并传递给主方法字符串数组参数。

Java中的命令行参数 (Command Line Arguments in Java)

Let’s say we have a simple java class to print command line arguments values.

假设我们有一个简单的java类来打印命令行参数值。

package com.journaldev.examples;

public class CommandLineArguments {

public static void main(String[] args) {

System.out.println("Number of Command Line Argument = "+args.length);

for(int i = 0; i< args.length; i++) {

System.out.println(String.format("Command Line Argument %d is %s", i, args[i]));

}

}

}

If we run this class without any arguments, the output will be as follows.

如果我们在不带任何参数的情况下运行此类,则输出将如下所示。

$ java com/journaldev/examples/CommandLineArguments.java

Number of Command Line Argument = 0

Now, let's pass some arguments to the main class. We have to pass the arguments as space-separated values.

现在,让我们将一些参数传递给主类。 我们必须将参数作为以空格分隔的值进行传递。

$ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C"

Number of Command Line Argument = 3

Command Line Argument 0 is A

Command Line Argument 1 is B

Command Line Argument 2 is C

$ java com/journaldev/examples/CommandLineArguments.java 1 2 3

Number of Command Line Argument = 3

Command Line Argument 0 is 1

Command Line Argument 1 is 2

Command Line Argument 2 is 3

$

Note: If you are using

注意 :如果使用的是

Java 11 or higher, you don't need to compile the java source file explicitly. The java command will compile and run the class simultaneously.

Java 11或更高版本,则无需显式编译Java源文件。 java命令将同时编译并运行该类。

如何在Eclipse中传递命令行参数 (How to Pass Command Line Arguments in Eclipse)

We can also pass command-line arguments to a program in Eclipse using Run Configurations.

我们还可以使用“运行配置”将命令行参数传递给Eclipse中的程序。

步骤1:打开“类运行配置”设置 (Step 1: Open the Class Run Configurations Settings)

From the class editor, right click and chose "Run As" -> "Run Configurations...".

在类编辑器中,右键单击并选择“运行方式”->“运行配置...”。

Eclipse Run Configurations

Eclipse运行配置

步骤2:在“参数”选项卡中指定程序参数 (Step 2: Specify the Program Arguments in the Arguments Tab)

In the pop up window, click on the Arguments tab. Then provide the command line arguments value in the "Program Arguments" text box.

在弹出窗口中,单击“参数”选项卡。 然后在“程序参数”文本框中提供命令行参数值。

Eclipse Command Line Arguments

Eclipse命令行参数

步骤3:点击“运行”按钮 (Step 3: Click on the Run button)

When you will click on the Run button, the run configurations will be saved and the program will execute with the specified command-line arguments.

当您单击“运行”按钮时,将保存运行配置,并且程序将使用指定的命令行参数执行。

Eclipse Command Line Arguments Example

Eclipse命令行参数示例

If you run the class again, the saved run configuration will be used. So if you want to override the command-line arguments or remove them, you will have to open the run configurations window and make necessary changes.

如果再次运行该类,将使用保存的运行配置。 因此,如果要覆盖或删除命令行参数,则必须打开运行配置窗口并进行必要的更改。

结论 (Conclusion)

The command-line arguments are used to provide values that are essential to run the program. For example, we can specify the database credentials to be used by the program. We can specify the configuration file location from where the program should pick the required values.

命令行参数用于提供运行程序必不可少的值。 例如,我们可以指定程序要使用的数据库凭据。 我们可以指定程序应从中选择所需值的位置的配置文件位置。

Reference: Command-Line Arguments Oracle Docs

参考: 命令行参数Oracle Docs

翻译自: https://www.journaldev.com/32004/command-line-arguments-in-java

java中的命令行参数

java 接收命令行参数_java中的命令行参数_Java中的命令行参数相关推荐

  1. Java方法01 方法(函数)定义、调用、值传递、重载、命令行传递参数

    Java 方法的定义.调用.值传递.重载.命令行传递参数 1. 什么是方法? 2. 方法的定义和调用 3. 值传递和引用传递 4. 方法的重载 5. 命令行传递参数 6. 可变参数(输入参数的数量不确 ...

  2. java中sql语句怎么把开始和结束时间作为参数写sql查询_java程序员跳槽的一道坎,大公司面试官都会问的Mybatis...

    一.什么是Mybatis? 1. Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动.创建连接.创建statement ...

  3. java中注解动态传参_Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)...

    Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)java 前言:因为前段时间忙于写接口,在接口中须要作不少的参数校验,本着简洁.高效的原则,便写了这个小工具供本身使 ...

  4. java 方法重载调用_Java方法的定义以及调用、方法重载、可变参数以及递归

    何谓方法 Java方法是语句的集合,它们在一起执行一个功能 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 System.out.println();/ ...

  5. java怎么给类中的私有变量赋值_Java核心技术笔记分享------第二章 类与对象

    对象与类 一.面向对象思想的概述 1>面向对象与面向过程: 二者都是一种思想,面向对象是相对于面向过程而言的.面向过程强调的是功能行为.面向对象,将功能封装进对象,强调具备了功能的对象. 面向对 ...

  6. java中date类型如何赋值_Java 中的类型传递问题解惑

    点击上方☝SpringForAll社区 轻松关注!及时获取有趣有料的技术文章 来源: https://blog.biezhi.me/2018/11/java-pass-by-value.html 我之 ...

  7. java中的main的介绍_Java中的main函数详细讲解

    第二.可以在另一个类中向包含main()的类传递参数,如下例: public class A { public static void main(String[] args) { for(int i= ...

  8. linux命令行中,双引号中的感叹号将被解释为历史命令

    linux命令行中,双引号中的感叹号将被解释为历史命令. 命令: test -e ttt/ && echo "file exist!" || echo " ...

  9. java中sc的快捷键,Windows下的SC命令参考

    C:\tmp>sc /? 错误:  未知命令 描述: SC 是用于与服务控制管理器和服务进行通信的命令行程序. 用法: sc [command] [service name] ... 选项 的格 ...

最新文章

  1. OpenAI机械手单手轻松解魔方,背靠强化学习+新技术ADR
  2. ways of make process to background job
  3. mysql 代理作业_查看SQLServer 代理作业的历史信息
  4. 2.2.5 操作系统之调度算法(时间片轮转调度算法、优先级调度算法、多级反馈队列调度算法)
  5. 常用DBA SQL[转]
  6. CCIE-LAB-第八篇-SDWAN-Branch1_Branch2_Vmanage
  7. android checkbox 选中事件_挖穿Android第四十九天
  8. nodejs操作sqlserver数据_SQL Server数据库损坏和修复
  9. VPP电源控制(VPP Power)-- 由DC-DC变换集成电路MC34063组成
  10. XML文件怎么转换成Excel表格文件
  11. java Apache poi 操作word生成word目录(根据word模板生成word文件)
  12. PS抠图小技巧-【色彩范围】
  13. 计算百分比的分析函数
  14. python修改系统时间_python修改操作系统时间的方法
  15. ssm+jsp计算机毕业设计车辆违章查询系统2hie7(程序+LW+源码+远程部署)
  16. java 序列化版本号_序列化版本号serialVersionUID的作用
  17. 某电子订单系统升级步骤
  18. 自编码器(Auto-encoder)的概念和应用
  19. INSEC WORLD丨【漏洞攻防与安全研究论坛】演讲实录精选
  20. 推荐7款超级好用的终端工具 —— SSH+FTP

热门文章

  1. Android中的asserts和res/raw资源目录
  2. Linux/Unix http://oreilly.com.cn/index.php?func=booklistcat=46
  3. mysql repeatableread_mysql-Innodb事务隔离级别-repeatable read详解(转)
  4. 新来的妹子把几百万数据放入了内存,系统立马爆了
  5. 加入ImportNew的翻译试题
  6. Word转换成PDF文件在线转换
  7. Matlab中的条件数
  8. 回文链表-python
  9. 最小长度电路板排列问题(C++实现)
  10. 以太坊基金会:Kiln 合并测试网上线公告