3

I am doing one project and I want to use JCommander for parsing command line input. But I don't know how to add it to my machine and use it.

Here is the GitHub page for JCommander. So how can I add it to my machine after downloading zip file of code?

I am not using any IDE now (using Sublime text and command line).

Bobulous
  • 12,967
  • 4
  • 37
  • 68
Pratik
  • 88
  • 1
  • 2
  • 11
  • How can i download jar file? It only has zip file. – Pratik Aug 25 '15 at 08:51
  • 1
    Sorry I was not aware that it contains only the sources. You can download the Jar from http://mvnrepository.com/artifact/com.beust/jcommander/1.48. – SubOptimal Aug 25 '15 at 09:02
  • And to add the jar file to the classpath from command line take a look at hits thread http://stackoverflow.com/questions/8084926/including-jar-files-in-class-path – mr. Holiday Aug 25 '15 at 09:07

3 Answers3

5

I just ran into the same question. Here is what worked for me.

  1. Download jcommander jar file from http://mvnrepository.com/artifact/com.beust/jcommander/latest , follow the link for "Artifact" on the table on that page. The dowloaded file is named jcommander-X.XX.jar.

  2. After download, say you are working with a file at path myJavaFiles/Example.java, create a directory myJavaFiles/lib and put the file there.

  3. Create a minifest.txt file

  4. So now you have the structure myJavaFiles ├── Example.java ├── lib │   └── jcommander-X.XX.jar └── manifest.txt

5 You can try this for a sample java file as Example.java

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

class Example {
  @Parameter(names={"--length", "-l"})
  int length;
  @Parameter(names={"--pattern", "-p"})
  int pattern;

  public static void main(String ... args) {
    Example ex = new Example();
    new JCommander();
    ex.run();
  }

  public void run() {
    System.out.printf("%d %d", length, pattern);
  }
}

6 And now putting it all together you can go two paths.

6.a Generate class files using this command javac -cp lib/jcommanderXX.XX.jar Example.java

Then run your program from the class file directly using java -cp lib/*:. Example -l 4 --pattern 5

OR if you want to create a self standing jar file...

6.b create a manifest.txt file and add classpath and lib paths there Class-Path: lib/jcommander-X.XX.jar Main-Class: Example After which you can compile using this javac -cp lib/jcommander-X.XX.jar Example.java

And finally run it with java -cp lib/*:. Example -l 4 --pattern 5

All of this ^^ is doable via the command line without the need to introduce gradle or maven builds, if you're just looking for simple way to work at command line this should work with other libraries too.

svict4
  • 432
  • 8
  • 24
Efrain
  • 66
  • 1
  • 2
1

You could still create a maven project even if you don't want to use an IDE. Then add following dependency in your pom.xml

<dependency>
  <groupId>com.beust</groupId>
  <artifactId>jcommander</artifactId>
  <version>1.48</version>
</dependency>

Then once you are done coding your application, you can do a mvn clean install in the terminal and you can run the jar created in the target folder using the java -jar option. Hope that helps. Nothing like using mvn from commandline. I would also recommend the maven assembly and maven jar plugins. They make your life easier. Take a look here for more.

Community
  • 1
  • 1
trailblazer
  • 1,421
  • 5
  • 20
  • 43
0

Version Update: by June 2017 the latest version is 1.72.

There are some major updates between 1.48 and 1.72. You will get confused if you are trying to learn from the latest official documentation. So you may want to include this instead:

<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.72</version>
</dependency>

For further maven version updates, you may want to click this link: https://mvnrepository.com/artifact/com.beust/jcommander