How to generate a checksum for jar file - java

We want to implement a feature like this: when user firstly run the app, app will download a jar file to local PC from a server and load the jar file but when user run the app again, app can distinguish if the content of the jar file in local folder is same as the one on server, if so not download it again just load it from local folder; if not app will download it and update local one (the jar file name on server and on local must be same). Currently one solution I can think out is to use checksum that is app generates the checksum of local jar file then get the checksum of server jar file from the server, check if they are same, if so not download again, if not download and update. Is there a simple way to generate checksum of a jar file? Or is there any other better solution for this feature? Thanks.

Calculating the jar checksum is the same as calculating the checksum of any file. But it looks like you need Java Web Start, which will take care of everything.

Java Web Start is certainly the right way to do it as Bozho says. For a hand-made alternative see What's the best way to add a self-update feature to a Java Swing application?

You can use the Files.getChecksum(file) from Google Guava libraries

If you are building the jar file with maven there is a maven plug-in for generating check-sums of a compiled jar. Maven Plugin Link .
As for the downloaded jar you can locally SAVE ,rather than generate, the jars check sum. Fetch the check-sum from the server and send a request to the it every time you need to check if the jars check sum is changed and if so download the new one.

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

Get current revision number of SVN working copy in java program

I'm trying to get the current revision number of the working copy to log it into the log file.
The software is written in Java. So how is it possible to access the revision number through Java?
It's unclear what kind of software you're writing - a webapp in a war? A standalone java program/client packaged in a jar? If you're creating a war or jar file, you can have your build put the SVN info into the META-INF/MANIFEST.MF and your code can read it from there.
use svnkit and use the folloing method:
SVNWCClient.doInfo()

using a library file from within jar

This is a follow up of this question
What I try to do:
I have an eclipse project that uses th Sigar library in order to get the cpu information (among others). In order to do that Sigar needs a library file to work. Each CPU/OS have a different file, all of which are available.I have to pack this into a jar, that other can use my application as a library.
What I did:
added the available library files to my project and can access them like so
System.setProperty("org.hyperic.sigar.path",System.class.getResource("/lib").getPath());
System.load(System.class.getResource(getClass().getResource(
"/lib/libsigar-amd64-linux.so").toString()).getPath());
Both of these seems to work if i run my application as a stand alone application.
When i pack it to a jar and try to run it from an other project, it just doesn't work. I cant make it to see the directory or the file.
This is the structure of my project
I have tried to access the file with any path i could think of, but all returned a NullPointerException.
How can I do this? Using a temp file? I am not sure how i should do this, i would like to load the whole folder and not just one file, since i dont know the hardware/OS of the client application.

Robocode: Where can I get a JAR file for the Engine?

I wish to automatically run a series of Robocode tanks in matches, and recieve back their scores and other info.
I believe that I require the Robocode Engine, and wondered where I could get a JAR file for using it in Java.
Thanks!
I have worked out the the Jar file that I need to use the methods from is actually inside the libs folder of the installed Robocode client. It's name is robocode.jar.
It just had to be added to the build path of my project as an external library

Categories

Resources