将命令行参数传递给runnable JAR [英] Passing on command line arguments to runnable JAR

查看:333
本文介绍了将命令行参数传递给runnable JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Eclipse项目构建了一个可运行的JAR,它处理给定的XML文件并提取纯文本。但是,此版本要求在代码中对文件进行硬编码。

I built a runnable JAR from an Eclipse project that processes a given XML file and extracts the plain text. However, this version requires that the file be hard-coded in the code.

有没有办法做这样的事情

Is there a way to do something like this

java -jar wiki2txt enwiki-20111007-pages-articles.xml

并让jar在xml文件上执行?

and have the jar execute on the xml file?

我做了一些环顾四周,所有给出的例子都与编译命令行上的JAR,没有处理传递参数。

I've done some looking around, and all the examples given have to do with compiling the JAR on the command line, and none deal with passing in arguments.

推荐答案

为什么不呢?

只需修改 Main-Class 即可接收参数并根据参数进行操作。

Just modify your Main-Class to receive arguments and act upon the argument.

public class wiki2txt {

    public static void main(String[] args) {

          String fileName = args[0];

          // Use FileInputStream, BufferedReader etc here.

    }
}

指定命令行中的完整路径。

Specify the full path in the commandline.

java -jar wiki2txt /home/bla/enwiki-....xml

这篇关于将命令行参数传递给runnable JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆