How do you download a a jar file with java? - java

I'm trying to implement an updates feature in my program. If an update is available, there will have to be one if not several different .jar files that must be downloaded. I'm having problems developing a way to download these files. I've looked at the URLConnection API as well as other API's related to this. I can download regular text files with no problem, but when I need to download a jar file it just doesn't download. No error is thrown, the data just isn't transferred.
If anyone can point me in the right direction to successfully download jar files I would greatly appreciate it.

There is nothing special about JAR files themselves. Transfer it just like any other file (FileInputStream -> SocketOutputStream: SocketInputStream->FileOutputStream). The trick is to use the ClassLoader to link the classes into your running program after you have saved the JAR to disk.

Related

Deployed spring boot jar file to Docker - how to access and change static resources

I'm kinda new to spring and web development as a whole.
My question is:
When you build a spring boot project (using Maven) into jar file and deploy it via Docker, everything is in one jar file. How can you access your resources (css, js, images, html...) if you want to edit something? Like change something in css file or add something to html page. Is it even possible? Or do you have to build a new jar file everytime, when you need to change something (in frontend)? Also, when there are being uploaded some images or other files, where are they stored? This stuff is very confusing for me and i can't find any related books or help at all.
Thanks for help!
when you package any java program it is nothing but a zip file. Based on what kind of package it is, you wither name it as a Jar or War.
Jar == Java archive
War == Web archive
Now, given the fact that jar and war both are essentially a zip archive, it gives you flexibility to extract and modify them just like any other zip file.
On windows, I think softwares like 7zip let you update the jar inline. I have done it multiple times, especially when I wanted to change application.properties alone on cloud machines, and no other code changes were required. In such cases, building the whole jar and transferring it again to cloud machine could be time consuming. So I would just extract the contents, update whatever I want to, and rezip the package.
Here is the commands you can use -
jar xf jar-file
This should extract the files into a directory.
This SO thread will guide you towards creating jar files.
Something like jar cf myJar.jar ** should be enough to generate a jar file IMO, but syntax might vary.
The jar file is actually just a zip file containing all the files and classes of your application, so technically you can change files in it like any other zip archive. Best practice is to build the jar file using Maven or Gradle from source every time you need something changed.
It's good practice to keep the source in version control using Git, and tag each build in the git repository - that way you can easily keep track of changes to the jar file by looking at what's in git at the time of the build.

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

Xuggler installation

I know this has been hashed over a bit on here, but I have an issue that hasn't been answered. I am attempting to install Xuggler plug it into Eclipse. I have no issues with the idea of adding it to my classpath, my issue is with getting my hands on the actual xuggle-xuggler-5.4.jar file. I've installed the one from the website, but there is only a zip folder, and not a jar file. I even tried to pack the file into a jar myself, but Eclipse wouldn't take it. I've looked everywhere in that zip folder and I haven't seen a jar file anywhere. Should just use the zip folder, or did I mess something up? I saw something where a guy had a xuggler file in his Program Files, but I don't have that.
EDIT:
I've been to the googleCode site given on the Xuggler website, and I've clicked on the one that says xuggle-xuggler-5.4.jar. For some reason, though, when I do this, it tries to install as a zip folder, not as a jar. Then it doesn't work in my classpath. When it installs as a zip, like it does in my case, there is no xuggle-xuggler-5.4.jar file on my computer. Instead, the file that was installed is now xuggle-xuggler-5.4.zip.
I don't use Eclipse myself, so I'm not familiar with your particular problem. I do use Xuggler however, and have successfully used the JAR found on their website. So here's just a couple of quick hints. I apologize if I am telling you things you already know.
You can find jar-builds for Xuggler here. Somehow they chose to hide the link on their website somewhat.
There have been previous questions on StackOverflow, such as this one, regarding instructions to add a JAR to an Eclipse project, which will automatically update the classpath and include the JAR during build.
JAR's are in a format that was extended from ZIP, as can be read here. It is unfair to say that JAR files and ZIP files are the same, but you could open a JAR file like you would a ZIP file and read (some) of its content.
Xuggler clearly states this on their website, but I missed it myself, so I thought I'd mention this. The xuggle-xuggler-5.4.jar can be used on any platform. Depending on the codecs you will be using, you might need some additional libraries. To save some disk space you could use xuggle-xuggler-noarch-5.4.jar and an appropriate xuggle-xuggler-arch-xxx.jar as a replacement for xuggle-xuggler-5.4.jar.

Cannot read from an runnable Jar file

I'm trying to make a mp3 player and I used javazoom libraries. I save mp3 path in library.txt file to reopen them. There is not any problem with openning library.txt which is in the jar file.
Normally with eclipse program It is working well but when i created a jar file the problems begins.
I can't use absolute paths to open any mp3 file with my jar file.
File file = new File("/Users/orcungumus/Music/iTunes/iTunes Media/Music/Bruno Mars/Unorthodox Jukebox/1-16 16 Locked Out of Heaven.mp3");
try {
player.open(file);
} catch (BasicPlayerException e1) {
JOptionPane.showMessageDialog(null, e1.toString(),
"Error", JOptionPane.INFORMATION_MESSAGE);
}
For making thinks easier to understand i used directly path of an file. It works for eclipse, but with jar file it can't open file.
This is the error which i take:
If it is important i use mac os.
Edit: I realized that this is not about absolute path by making mp3 path relative. Libraries give an error about audio format. So, the problem is still exist. what can be the differencies between runnable jar file version and eclipse run for an project.
well, that's hard to find out.
now your problem is unable to open a audio file, which I can't handle.
However you said that the programme run perfectly using eclipse, so the question became
the different between running in eclipse and normal jar file.
FIRST, eclipse not directly use java.exe nor javaw.exe for execution. Instead, eclipse use some custom defined library in order to redirect the console, handling exception, etc.
(That is nothing to your problem)
SECOND, eclipse will set the working directory for you automatically (to the project root directory), where may contain some necessary native library.
THIRD, eclipse also define some path for native location for library, I think you may use eclipse built-in export to create the jar file which however not include the native library.
the second and third difference may help you, because they are quite effective to jar file.
I have just browsed javazoom briefly, I don't know whether there is native library. If not,
I sorry that you have to find other solution.

Java cannot save to a resource folder with jar

I am writing a program which is dependent on saving to a resource folder that is exported with the jar. I have a source folder titled "resources/inputs" and it exports correctly. I can load from it which is great, but the problem is, when I use:
getClass().getResource(path);
I cannot save to the path returned. I need to be able to save to it and I was wondering, is there any way I can save to this resource folder (or to some other folder existent in the same directory as the jar) no matter where the user has saved the jar file?
The error I get is a FileNotFoundException and the background I've read on it is that since jar triggers java "read-lock" you can only ever read from a jar and can never write to it? Not sure if that is accurate or not, but if it is, how can I work around this using an external folder?
I've never tried this, but its probably having a hard time because the you need to use specialized classes to access files within a jar file (which is a zip file with a different extension). Google "JarEntry" and see if that points you in the right direction.
Does this answer your question

Categories

Resources