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

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

问题描述

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

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

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

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