从Eclipse发送命令行参数

时间:2013-08-09 21:48:24

标签: java eclipse

public static void main(String[] args) throws FileNotFoundException, SVGFormatException {
    SVG svg = new SVG("test.svg"); //so that the file called test.svg can be called upon
    PortablePixmap ppm = new PortablePixmap(500, 500); //select the size of the pixmap(canvas)

    svg.drawPixmap(ppm); //draw on ppm

    ppm.writeToFile("out.ppm"); //save the file as out.ppm file 

这是我编写的代码,但我需要从命令行输入中获取这些值,因为如果我像这样硬编码,用户就无法选择他们想要使用的值。能否帮助我如何从命令行输入中获取这些值?

2 个答案:

答案 0 :(得分:0)

String []传递给main()方法时,您已经可以使用命令行参数。

因此,如果您将程序作为

运行
java YourProgram test.svg out.ppm

您可以将这些参数视为

public static void main(String[] args) throws FileNotFoundException, SVGFormatException {
    SVG svg = new SVG(args[0]); // test.svg
    PortablePixmap ppm = new PortablePixmap(500, 500);

    svg.drawPixmap(ppm); //draw on ppm

    ppm.writeToFile(args[1]); // out.ppm
}

因为你正在使用Eclipse,你必须转到

  

右键单击>运行方式>运行配置>参数选项卡

test.svg out.ppm下添加Program arguments。点击Apply,然后点击Run

答案 1 :(得分:0)

点击Run按钮旁边的小箭头,然后选择run configurations -> (x) = arguments标签,在Program arguments:文本框下方点击variables按钮,然后点按此处。

下次运行程序时,会出现一个对话框提示您输入参数。

现在,在您的程序中,您可以通过解析main方法的String[] args参数来处理这些参数。