简体   繁体   中英

Simplest way to download a jar from the command line given the fully qualified name of a class

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 eg Maven Central.

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.

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.

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 (eg) python program to do something, but it would not be very useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM