0

For example, if I know that I have to do the following import in my program

import org.apache.commons.math3.distribution.NormalDistribution;

what is the fastest/simplest way to determine in command-line what is the package containing this class and download its jar for the current version?

I read this question and it is about Eclipse and every answer assumes you already have the jar file you're looking for, not going into details about downloading it though e.g. Maven Central.

Community
  • 1
  • 1
gsmafra
  • 2,434
  • 18
  • 26
  • Are you trying to do this through Maven? I can put a step by step process together but I find the scope of your question very broad. – ProgrammersBlock Dec 10 '16 at 03:29
  • Not necessarily through Maven but I imagined it could be a way to do it. – gsmafra Dec 10 '16 at 03:37
  • Possible duplicate of [Java: How do I know which jar file to use given a class name?](http://stackoverflow.com/questions/275120/java-how-do-i-know-which-jar-file-to-use-given-a-class-name) – Naman Dec 10 '16 at 06:03

3 Answers3

1

On the one hand, the answer is very simple and on the other it isn't. The easiest way with almost no setup is to download the jar directly from the project site, in your case this one. If you can't find the project host, you may search on Maven Central. Don't forget to add the jar to your classpath after the download.

A complete command line solution requires a build tool like Maven or Gradle. They require some setup but once set up, you just tell them which library you need and they'll do the download for you.

vatbub
  • 2,713
  • 18
  • 41
  • So, if I want a complete command line solution, which tool is the simplest to setup/use for this use case and what are the exact steps I have to do? – gsmafra Dec 10 '16 at 03:41
  • I personally prefer Maven. To use maven, you need to [download](https://maven.apache.org/download.cgi) and [install](http://maven.apache.org/install.html) maven. Then you'll need to tell maven your project settings using a so called `pom.xml`-file. I found [this guide](https://spring.io/guides/gs/maven/) on the internet that explains that step quite good. – vatbub Dec 10 '16 at 03:51
1

You should be able to "curl" grepcode.com's website, parse the results to identify the jar file. By parse, I mean, you may have to write your own code to parse the results by command line. Jsoup can help with a lot of this.

Example class

http://grepcode.com/search/?query=org.apache.commons.math3.distribution.NormalDistribution

Jar file -> commons-math3

Then "curl" search.maven.org using the advanced search. Parse the results, identify the link you need.

http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22commons-math3%22

You can play around with the advanced search here. http://search.maven.org/#advancedsearch%7Cgav

Be careful as sometimes the same jar file is listed multiple times on search.maven.org. So I gravitate towards the ones that supply source jar or have the most versions.

ProgrammersBlock
  • 5,974
  • 4
  • 17
  • 21
1

There is no simple solution. Hundreds of different artifacts in Maven Central can contain a particular class. How would your command line decide which one to give you?

As per this link, there is a REST API to search.maven.org. You can use this to retrieve the (long) list of artifacts for a class. So, you could write an (e.g.) python program to do something, but it would not be very useful.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Maybe sorting by popularity, then letting the user choose, with the most popular one being the default? – gsmafra Dec 10 '16 at 15:45