命令行参数具体是什么?

发布于 2024-12-28 08:39:58 字数 212 浏览 4 评论 0原文

我一直想问我的大学老师它在 public static void main(String[] args) 中具体做了什么,但我实际上从来没有机会,因为那是上学期的事,我从来没有真正了解过它的作用做什么以及它的重要性是什么,我们只是因为实践而使用。但当我尝试学习 C# 时,出现了此类问题(我试图从 MSDN 教程中学习 C#,但命令行参数的定义并不那么深入和密集)。什么是命令行参数?

I always wanted to ask my college instructor what does it do specifically in public static void main(String[] args) but I never actually had a chance because it was last term, I never actually what does it do and what its importance, we just use because of practice. but as I try to learn C# these type of question arises, (I am trying to learn C# from the MSDN tutorials but the definition of command line argument is not that depth and dense) . what is a command line argument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

春夜浅 2025-01-04 08:39:58

主可执行文件名之后传递的任何内容:

helloworld.exe arg1 arg2 "arg 3"

在这种情况下,控制台将解释 arg1arg2 > 作为前两个参数,"arg 3" 作为第三个参数。 CLI(在本例中为命令行解释器)将使用空格作为分隔符。

另请记住,如果可执行文件的路径包含空格:

hello world.exe

那么命令行解释器通常会将 hello 解释为命令,其中 world.exe< /code> 作为第一个参数;因此,在这种情况下,引号对于实际描述命令结束和参数开始的位置非常重要。

您的应用程序无法控制如何解析引号;因此,如果您需要拥有自己的参数引用系统(例如,如果您将运行时参数打包到单个命令行参数中),您可能必须构建自己的解析器并使用自己的引用系统;例如使用 ' 甚至括号。

Anything that is passed after the main executable filename:

helloworld.exe arg1 arg2 "arg 3"

In this case, the console will interpret arg1 and arg2 as the first two arguments, and "arg 3" as the third. CLIs (in this case Command Line Interpreters) will use space as a delimiter.

Remember also that if the path of the executable contains spaces:

hello world.exe

Then a command line interpreter typically interprets hello as the command, with world.exe as the first argument; thus in this case the quotes are important to actually delineate where the command ends and the arguments begin.

Your application has no control as to how the quotes are parsed; therefore if you need to have your own quoting system for arguments (e.g. if you are packing runtime parameters into a single commend-line argument) you will likely have to build your own parser and use your own quoting system; e.g. use ' or even brackets.

甜`诱少女 2025-01-04 08:39:58

命令行参数是从命令行(“DOS 提示符”)调用时传递给可执行文件的参数,例如

C:\Users\dtb>MyProgram.exe These are command line arguments

此外,当您将文件扩展名(例如 .foo)与可执行文件关联时,文件名将传递为双击文件时可执行文件的命令行参数。

A command line argument is an argument passed to the executable when invoked from the command line ("DOS prompt"), e.g.

C:\Users\dtb>MyProgram.exe These are command line arguments

Also, when you associate a file name extension (such as .foo) with your executable, the file name is passed as command line argument to the executable when you double-click the file.

第七度阳光i 2025-01-04 08:39:58

命令行参数是程序调用的字符串。以:ping computerA 为例。 ComputerA 将是 ping 的命令行参数。

Command line arguments are the Strings that are called with the program. Take for example: ping computerA. ComputerA would be the command line argument for ping.

携君以终年 2025-01-04 08:39:58

如果我使用 IDE,我的程序还能运行吗?

AFAIK,所有 IDE 都支持带有命令行参数的程序。

如果我删除 (String[] args)?

一个方法

public static void main(String[] args) 

当您开始时,仅调用或

public static void main(String... args) 

调用

,因为这是预期的。如果你有一个方法,

public static void runme(String... args) // won't be called.
public int main(String... args) // won't be called.
public static int main(String... args)  // won't be called.
public static void main()  // won't be called.

这些方法不是同一个方法,你会得到一个错误。

Will my program still work if I am using an IDE?

AFAIK, All IDEs support program with command line arguments.

if I remove (String[] args)?

When you start, only a method called

public static void main(String[] args) 

or

public static void main(String... args) 

is called because that is what is expected.

If you have a method

public static void runme(String... args) // won't be called.
public int main(String... args) // won't be called.
public static int main(String... args)  // won't be called.
public static void main()  // won't be called.

these are not the same method and you will get an error.

梦纸 2025-01-04 08:39:58

这些是您可以在应用程序启动时传递的参数。它们之间用空格分隔。
请参阅此处:http://en.wikipedia.org/wiki/Command-line_interface

These are parameters you can pass your application at startup. They are seperated with spaces.
See here: http://en.wikipedia.org/wiki/Command-line_interface

但可醉心 2025-01-04 08:39:58

是的,您向应用程序传递了一些参数。假设我将像这样运行你的程序:

java -jar yourApplication.jar Pavel Janicek

然后

args[0] = "Pavel"
args[1] = "Janicek"

你可以稍后打印出来,或者用作你的程序的变量

Yes, you pass your application some arguments. Say I will run your program like this:

java -jar yourApplication.jar Pavel Janicek

then

args[0] = "Pavel"
args[1] = "Janicek"

you can print out these later on, or use as variables for your program

内心激荡 2025-01-04 08:39:58

命令行参数是您在运行程序时提供的参数。就像 java -jar myprogram arg1 arg2 或例如在 java 中运行 java myprogram arg1 arg2 而不是 java myprogram 时。

因此,下面的代码:

for (String arg : args) 
System.out.println(arg);

输出将为 arg1 arg2

这意味着第一个参数 = arg1,第二个参数 = arg2

Command line arguments are those you supply when you run your program. Like java -jar myprogram arg1 arg2 or for example in java when you run java myprogram arg1 arg2 instead of java myprogram.

So, the below code:

for (String arg : args) 
System.out.println(arg);

The output will be arg1 arg2

It means first argument = arg1 and second argument = arg2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文