如何将变量从命令提示符传递到我的Java程序

发布于 2025-02-08 04:25:36 字数 126 浏览 3 评论 0 原文

有没有办法从命令提示符中读取数据?我有一个Java程序,该程序依赖于外部源的4个输入变量。在运行JavaScript程序后,这些变量将返回命令提示符,但是我需要一种将这些变量从命令提示符传递到我的Java程序的方法,任何帮助都将不胜感激!

Is there a way to read data from the command prompt? I have a java program that relies on 4 input variables from an outside source. These variables are returned to the command prompt after I run a javascript program but i need a way to pass these variables from the command prompt into my java program, any help would be greatly appreciated!

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

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

发布评论

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

评论(2

二手情话 2025-02-15 04:25:36

执行Java程序通过参数时,所有参数应通过空间分开。

Java程序名参数1参数2参数3参数4

此参数可在您的主要方法参数中可用

 public static void main(String[] args){
     //This args array would be containing all four values, i.e. its length would be 4 and you easily iterate values.
    for(int i=0; i<args.length; i++){
        System.out.println("Argument " + i + " is " + args[i]);

}

While executing java program pass the parameters and all the parameters should be separated by space.

java programName parameter1 parameter2 parameter3 parameter4

This parameters would be available in your main method argument

 public static void main(String[] args){
     //This args array would be containing all four values, i.e. its length would be 4 and you easily iterate values.
    for(int i=0; i<args.length; i++){
        System.out.println("Argument " + i + " is " + args[i]);

}
惟欲睡 2025-02-15 04:25:36

请点击链接:

命令行参数 - Java™教程:

@backslash共享。

它具有所有的内容,可以帮助您清除所有疑问。
链接中的内容如下:

显示用户从命令行传递到Java程序的命令行参数

以下示例将其每个命令行参数显示在一个
单行本身:

  public class displayCommandlineParameters {
    公共静态void main(string [] args){
        对于(字符串S:args){
            system.out.println(s);
        }
    }
}
 

要编译程序:从命令提示符中,导航到包含.java文件的目录,例如C:\ test,通过键入CD
命令下面。

c:\ users \用户名&gt; cd c:\ test
C:\ test&gt;

假定文件,例如DisplayCommandlineParameters.java,在
当前工作目录,在下面键入Javac命令进行编译。

c:\ test&gt; javac displayCommandlineParameters.java
C:\ test&gt;

如果一切顺利,您应该看到没有错误消息。

运行程序:以下示例显示了用户如何运行类。

c:\ test&gt; java displaycommandlineparameters hello java world

输出:
你好
Java
世界

请注意,该应用程序显示每个单词 - 您好,Java和World-
在一条线上。这是因为空间特征分开
命令行参数。

打招呼,​​爪哇和世界被解释为一个论点,
用户将通过将它们包装在引号中加入。

c:\ test&gt; java displayCommandlineParameters“ Hello Java world”

输出:你好Java World

Follow the link:

Command-Line Arguments - The Java™ Tutorials : https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

shared by @BackSlash.

It has all the content which would help you to clear all your doubts.
The content from the link is quoted below:

Displaying Command-Line Arguments passed by user from command-line to a Java program

The following example displays each of its command-line arguments on a
line by itself:

public class DisplayCommandLineParameters {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}

To compile the program: From the Command Prompt, navigate to the directory containing your .java file, say C:\test, by typing the cd
command below.

C:\Users\username>cd c:\test
C:\test>

Assuming the file, say DisplayCommandLineParameters.java, is in the
current working directory, type the javac command below to compile it.

C:\test>javac DisplayCommandLineParameters.java
C:\test>

If everything went well, you should see no error messages.

To run the program: The following example shows how a user might run the class.

C:\test>java DisplayCommandLineParameters Hello Java World

Output:
Hello
Java
World

Note that the application displays each word — Hello, Java and World —
on a line by itself. This is because the space character separates
command-line arguments.

To have Hello, Java and World interpreted as a single argument, the
user would join them by enclosing them within quotation marks.

C:\test>java DisplayCommandLineParameters "Hello Java World"

Output: Hello Java World

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