Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing on command line arguments to runnable JAR [duplicate]

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.

like image 657
Jason Avatar asked Nov 14 '11 14:11

Jason


1 Answers

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 
like image 141
Kal Avatar answered Oct 25 '22 14:10

Kal