简体   繁体   English

如何在Java中传递参数

[英]How to pass argument in java

Hi i am using this code to run the shell script.the thing is i want to pass argument "trng-java" while i am running program.like this 嗨,我正在使用此代码运行shell脚本。问题是我想在运行program.like时传递参数“ trng-java”

like java Classname trng-java

code: 码:

import java.io.*;

public class Test
{


public static void main(String[] args)
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process pr = rt.exec(new String[]{"/bin/sh", "/tmp/test.sh", "trng-java"});

        BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
    } 
    catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}
}

How to do this? 这个怎么做?

您传入的参数将在args参数中以String yourParam = args [0]的形式出现

命令行参数通过String []参数args传递到您的应用程序,因此您应该能够使用args[0]访问该值。

Process pr = rt.exec(new String[]{"/bin/sh", "/tmp/test.sh", args[0] });

Process pr = rt.exec(new String[]{"/bin/sh", "/tmp/test.sh", args[0]});

当然,您还需要检查args是否包含至少一个参数

When you pass arguments to a java application, they are accessible through the args[] array in main. 当您将参数传递给Java应用程序时,可以通过main中的args[]数组对其进行访问。

See: http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html which I am quoting: 请参阅: http : //docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html我在此引用:

Echoing Command-Line Arguments 回显命令行参数

The Echo example displays each of its command-line arguments on a line by itself: Echo示例将其每个命令行参数单独显示在一行上:

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

The following example shows how a user might run Echo. 以下示例显示用户如何运行Echo。 User input is in italics. 用户输入以斜体显示。

 java Echo Drink Hot Java Drink Hot Java 

Parsing Numeric Command-Line Arguments 解析数字命令行参数

If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. 如果应用程序需要支持数字命令行参数,则必须将代表数字(例如“ 34”)的String参数转换为数字值。 Here is a code snippet that converts a command-line argument to an int: 这是一个将命令行参数转换为int的代码片段:

 int firstArg; if (args.length > 0) { try { firstArg = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Argument" + " must be an integer"); System.exit(1); } } 

只需将参数添加到您的命令中,如下所示:

rt.exec("/bin/sh /tmp/test.sh trng-java");

in public static void main(String[] args) the args parameter is the arguement. public static void main(String[] args) ,args参数是争论。 If you use multiple arguement like 如果您使用多种争论

java Classname a b c

Then in your main method args will be an array containing a,b,c 然后在您的主要方法中args将是一个包含a,b,c的数组

if this is what you want then args[0] is the preferred arguement. 如果这是您想要的,则首选args[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM