Passing on command line arguments to runnable JAR

Jason picture Jason · Nov 14, 2011 · Viewed 116.6k times · Source

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

and have the jar execute on the xml file?

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.

Answer

Kal picture Kal · Nov 14, 2011

Why not ?

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