5

Is there any way to download a jar file from a maven repository using java, or any other language?

In a Maven project, when I add a dependency it usually downloads the jar files from a remote repository if it does not exist on the local system.

Is there any way to do that, without using Maven, like in a library, or construct a URL and then fetch the jar?

4
  • 3
    It all boils down to a constructed URL. The advantage of using maven is that it understands mirrors if configured, etc. What is the underlying problem you need to solve? Commented May 15, 2022 at 14:39
  • "fetch the JAR" - You want to download just the one JAR file - without any of its dependencies? You may be able to build the required download URL, if you know the dependency details (and maybe also the JAR file name if it doesn't correspond to the dependency coordinates). Look at the jar link on a typical Maven page such as this one. I don't know why you would want to do this, or how useful it would be, without its transitive dependencies... You may end up just writing your own "Maven-Lite" app. Commented May 15, 2022 at 14:41
  • 1
    Not meant as a criticism at all, but I'm also wondering what motivates the desire to do this? Do you have some specific use case?
    – skomisa
    Commented May 15, 2022 at 16:42
  • Well i have no use case for this, I was just wondering how to do it.
    – Forsythe
    Commented May 16, 2022 at 3:56

4 Answers 4

5
  • Navigate in a browser to https://mvnrepository.com/
  • Identify your library. (Enter the library name or a related topic into the search box. Then select it by clicking on it.)
  • On the library's page you have to click on the particular version you want.
  • On the version page there is a table row named Files which contains the links to the .pom and .jar files.

A Java application to download a file from an URL:

import java.io.*; import java.net.*; import java.nio.file.*;
public class Download {
    public static void main(String[] args) throws MalformedURLException, IOException{
        String url = args[0];
        String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
        try(InputStream in = new URL(args[0]).openStream()) {
                Files.copy(in, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
        }  
    }
}
$ java Download.java https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/3.36.0.3/sqlite-jdbc-3.36.0.3.jar
$ ls sqlite-jdbc-3.36.0.3.jar 
sqlite-jdbc-3.36.0.3.jar
$ 

Other than Java everything else works as fine for the download, i.e. download via your browser or via a command line tool like curl:

$ curl https://repo1.maven.org/maven2/org/json/json/20220320/json-20220320.jar --output json-20220320.jar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 70939  100 70939    0     0   339k      0 --:--:-- --:--:-- --:--:--  348k
1

You can easily download any jar or pom files from public repos, for example

https://repo1.maven.org/maven2/...
<!-- Example -->
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-starter/2.6.7/

Downloading file in Java is pretty straightforward, there numerous ways to do so when you have URL.

0

Usually you will find the information related to the downloads directly in the repository web page, most of the time that information is in maven website too, maven was built precisely to tackle errors with downloaded jars though

0

Let's say you want to download:

<dependency>
  <groupId>org.eclipse.jetty.ee10.websocket</groupId>
  <artifactId>jetty-ee10-websocket-jakarta-server</artifactId>
  <version>12.0.7</version>
</dependency>

If you have mvn downloaded (you can even leave it in your downloads folder...) the run:

mvn dependency:copy -Dartifact=org.eclipse.jetty.ee10.websocket:jetty-ee10-websocket-jakarta-server:12.0.7 -DoutputDirectory=test/

It will download what you want.

1
  • it doesn't download that jar and all their dependencies Commented Jun 2 at 5:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.