This question already has an answer here:
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.
When you run your application this way, the java excecutable read the MANIFEST inside your jar and find the main class you defined. In this class you have a static method called main. In this method you may use the command line arguments.
You can pass program arguments on the command line and get them in your Java app like this:
Alternatively you pass a system property by changing the command line to:
java -Dpath-to-xml=enwiki-20111007-pages-articles.xml -jar wiki2txt
and your main class to:
Why not ?
Just modify your
Main-Class
to receive arguments and act upon the argument.Specify the full path in the commandline.
You can also set a Java property, i.e. environment variable, on the command line and easily use it anywhere in your code.
The command line would be done this way:
and the java code accesses the value like this:
See this question about argument passing in Java.