#how to download a jar file using java urlconnection

1 messages · Page 1 of 1 (latest)

carmine crescentBOT
#

<@&987246554085740594> please have a look, thanks.

grim lake
#

First of, don't do unsafe casts

#

Huh

#

Where did the message go ?

viscid light
#

How can i download a jar file using java what i already tried is


 // final URL jarUrl = new URL("jar:https://repo.maven.apache.org/maven2/org/springframework/spring-web/6.1.12/spring-web-6.1.12.jar!/");

    private void download(URL jarUrl, String directory) throws IOException { 
        final String[] splittedUrl = (jarUrl.toString().split("/"));
        final String fileName = splittedUrl[splittedUrl.length - 1];
        final File file = new File(directory + fileName);
        final JarURLConnection jarUrlConnection = (JarURLConnection) jarUrl.openConnection();

        
        final InputStream inputStream = jarUrlConnection.getInputStream();
        final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        try (final FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            int readByte = inputStreamReader.read();
            while (readByte != -1) {
                DATA_CALCULATOR.incrementByte();
                DATA_CALCULATOR.printOnEachMbDownload();
                fileOutputStream.write(readByte);
                readByte = inputStreamReader.read();
            }
        }
    }``` the above code results in an io exception with message ``` no entry name specified ``` now following java docs ```if there is no context URL and the specification passed to the URL constructor doesn't contain a separator, the URL is considered to refer to a JarFile.```  again if remove the separator !/ it result in a malformed url exception with message ``` add a separator```  does anybody know a way ?
carmine crescentBOT
viscid light
grim lake
grim lake
#

Second of, you are asking to just download a file, so just

try(InputStream in = url.openConnection()) {
    Files.copy(in, Path.of("my file.txt));
}
viscid light
viscid light
#

normally when a .jar file is converted to a .zip file it gives you access to all the sub directories where you can find all the .class files but if i download a .jar file following your method it does not

grim lake
viscid light
#

first i want to download a jar and then extract it

grim lake
#

Right

grim lake
#

Then use JarFile or ZipFile to extract it

viscid light
viscid light
# grim lake Then use JarFile or ZipFile to extract it

can i ask java final String[] splittedUrl = (jarUrl.toString().split("/")); final String fileName = splittedUrl[splittedUrl.length - 1]; final File file = new File(directory + fileName); final URLConnection urlConnection = jarUrl.openConnection(); final InputStream inputStream = urlConnection.getInputStream(); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream); try (final FileOutputStream fileOutputStream = new FileOutputStream(file)) { int readByte = inputStreamReader.read(); while (readByte != -1) { fileOutputStream.write(readByte); readByte = inputStreamReader.read(); } } what is wrong with this

grim lake
viscid light
#

i used it