#how to download a jar file using java urlconnection
1 messages · Page 1 of 1 (latest)
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 ?
Detected code, here are some useful tools:
do you consider it as an unsafe cast
So first of, don't do unsafe cast
Yes?
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));
}
ok this works but there is a problem a jar file downloaded this way does not contain any sub directories
Why ?
A jar is a jar
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
Wdym, do you want to download a jar or do you want to extract a jar?
first i want to download a jar and then extract it
Right
Then first do this to download it
Then use JarFile or ZipFile to extract it
ok
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
this is the error i get when i download a jar file with the method provided above
Why don't you use my code?