Login Register






Update Feature in a Java Tool filter_list
Author
Message
Update Feature in a Java Tool #1
Hello All,


I have a doubt and that is how to make a update feature in a tool I make with java. Its is feature that is present in most of the well known software's available to us. I want it to be a feature in my future tools that I make in Java.

Suppose I have a menu option like "Check for updates.." and now if an update is found then it is download and is added to the tool.

Please help me in this regard.

EDIT : I forgot to mention but I know about the JNLP and Java WebStart. But its for network launching and can be embedded in a web page. But how can this be done in a standalone application.

Thank you.
[Image: OilyCostlyEwe.gif]

Reply

RE: Update Feature in a Java Tool #2
From https://stackoverflow.com/questions/1748...are-update
Quote:
  • the user starts the applicataion
  • the application launches an "updater" (another program)
  • the updater retrieves from the Internet if a newer version exists
  • if that's the case, propose the user to update
  • the users accepts, the updater downlads the new install package (that can be incremental)
  • the updater shuts the application down (or better, asks the user to do it) and launches the new installer.
  • the installation package do the rest

Updating a runnable JAR is relatively easy. Java's standard library supports to update files in a jar.
Of course the most easy way is to replace the entire program.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Update Feature in a Java Tool #3
(03-26-2014, 07:54 PM)Deque Wrote: From https://stackoverflow.com/questions/1748...are-update
Quote:
  • the user starts the applicataion
  • the application launches an "updater" (another program)
  • the updater retrieves from the Internet if a newer version exists
  • if that's the case, propose the user to update
  • the users accepts, the updater downlads the new install package (that can be incremental)
  • the updater shuts the application down (or better, asks the user to do it) and launches the new installer.
  • the installation package do the rest

Updating a runnable JAR is relatively easy. Java's standard library supports to update files in a jar.
Of course the most easy way is to replace the entire program.

I understand this part but how can I integrate it with a javatool.

Suppose I use install4j and create an installer and install in my system or even if I make a .deb or rpm package and install it in my system then how to check for the updates and then add the new features to it.

Quote:Java's standard library supports to update files in a jar.

Can you redirect me, maybe I have not come across it and even if I have then I am missing a few threads.

I found this : http://stackoverflow.com/questions/40024...at-runtime

the explanation is good.

Also got this :- http://wiki.eclipse.org/Equinox/p2/Addin...pplication
[Image: OilyCostlyEwe.gif]

Reply

RE: Update Feature in a Java Tool #4
Here is an answer that might help you: https://stackoverflow.com/questions/3110...at-runtime

I also wrote similar code for my jarvirus, but it would need some modifications to work for you. So you probably better go with the code above. Nevertheless, my code:

Spoiler:
In updateJar delete the hostName parameter and the calls to writeHostName and copyHost. You probably would have to modify more, so yeah, use the code in the link I gave you.

Code:
package jarvir; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Map.Entry; import java.util.Set; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; public class Jarchiver { private final String jarFile; private Manifest manifest; public static void main(String[] args) { System.out.println("Success: Jarchiver as dummy host file started"); } public Jarchiver(String jarFile) throws IOException { this.jarFile = jarFile; try (JarInputStream is = new JarInputStream( new FileInputStream(jarFile))) { manifest = is.getManifest(); } } private void printAttributes(Manifest man) { Attributes attr = man.getMainAttributes(); for (Entry<Object, Object> entry : attr.entrySet()) { System.out.println(entry); } } public String readEntryPoint() { Attributes attr = manifest.getMainAttributes(); return attr.getValue(Attributes.Name.MAIN_CLASS); } public void changeEntryPoint(String entryPoint) { Attributes attr = manifest.getMainAttributes(); attr.put(Attributes.Name.MAIN_CLASS, entryPoint); printAttributes(manifest); } public void updateJar(Set<String> inFiles, String ownJar, String hostName) throws FileNotFoundException, IOException { String jarOut = jarFile + "out.jar"; try (JarInputStream jin = new JarInputStream(new FileInputStream( jarFile)); JarInputStream ownIn = new JarInputStream(new FileInputStream( ownJar)); JarOutputStream jout = new JarOutputStream( new FileOutputStream(jarOut), manifest)) { copyHost(jin, jout); writeInFiles(inFiles, ownIn, jout); writeHostName(jout, hostName); } replace(jarFile, jarOut); } private void writeHostName(JarOutputStream out, String hostName) throws IOException { out.putNextEntry(new JarEntry("jarvir/host")); out.write(hostName.getBytes()); out.closeEntry(); } private void writeInFiles(Set<String> inFiles, JarInputStream ownIn, JarOutputStream jout) throws IOException { JarEntry entry; while ((entry = ownIn.getNextJarEntry()) != null) { if (inFiles.contains(entry.getName())) { writeJarEntry(jout, ownIn, entry); System.out.println("write own entry " + entry); } } } private void copyHost(JarInputStream jin, JarOutputStream jout) throws IOException { JarEntry entry; while ((entry = jin.getNextJarEntry()) != null) { writeJarEntry(jout, jin, entry); System.out.println("write entry " + entry); } } private void writeJarEntry(JarOutputStream out, JarInputStream in, JarEntry entry) throws IOException { out.putNextEntry(entry); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } out.closeEntry(); } private void replace(String toReplace, String replacement) throws IOException { Path source = Paths.get(replacement); Path target = Paths.get(toReplace); Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); } }



Other than that the explanation in the link you found about updating jars was pretty good. I couldn't have done it better. What is it you need now?
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Update Feature in a Java Tool #5
(03-27-2014, 08:05 AM)Deque Wrote: Here is an answer that might help you: https://stackoverflow.com/questions/3110...at-runtime

I also wrote similar code for my jarvirus, but it would need some modifications to work for you. So you probably better go with the code above. Nevertheless, my code:

Spoiler:
In updateJar delete the hostName parameter and the calls to writeHostName and copyHost. You probably would have to modify more, so yeah, use the code in the link I gave you.

Code:
package jarvir; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.Map.Entry; import java.util.Set; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; public class Jarchiver { private final String jarFile; private Manifest manifest; public static void main(String[] args) { System.out.println("Success: Jarchiver as dummy host file started"); } public Jarchiver(String jarFile) throws IOException { this.jarFile = jarFile; try (JarInputStream is = new JarInputStream( new FileInputStream(jarFile))) { manifest = is.getManifest(); } } private void printAttributes(Manifest man) { Attributes attr = man.getMainAttributes(); for (Entry<Object, Object> entry : attr.entrySet()) { System.out.println(entry); } } public String readEntryPoint() { Attributes attr = manifest.getMainAttributes(); return attr.getValue(Attributes.Name.MAIN_CLASS); } public void changeEntryPoint(String entryPoint) { Attributes attr = manifest.getMainAttributes(); attr.put(Attributes.Name.MAIN_CLASS, entryPoint); printAttributes(manifest); } public void updateJar(Set<String> inFiles, String ownJar, String hostName) throws FileNotFoundException, IOException { String jarOut = jarFile + "out.jar"; try (JarInputStream jin = new JarInputStream(new FileInputStream( jarFile)); JarInputStream ownIn = new JarInputStream(new FileInputStream( ownJar)); JarOutputStream jout = new JarOutputStream( new FileOutputStream(jarOut), manifest)) { copyHost(jin, jout); writeInFiles(inFiles, ownIn, jout); writeHostName(jout, hostName); } replace(jarFile, jarOut); } private void writeHostName(JarOutputStream out, String hostName) throws IOException { out.putNextEntry(new JarEntry("jarvir/host")); out.write(hostName.getBytes()); out.closeEntry(); } private void writeInFiles(Set<String> inFiles, JarInputStream ownIn, JarOutputStream jout) throws IOException { JarEntry entry; while ((entry = ownIn.getNextJarEntry()) != null) { if (inFiles.contains(entry.getName())) { writeJarEntry(jout, ownIn, entry); System.out.println("write own entry " + entry); } } } private void copyHost(JarInputStream jin, JarOutputStream jout) throws IOException { JarEntry entry; while ((entry = jin.getNextJarEntry()) != null) { writeJarEntry(jout, jin, entry); System.out.println("write entry " + entry); } } private void writeJarEntry(JarOutputStream out, JarInputStream in, JarEntry entry) throws IOException { out.putNextEntry(entry); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { out.write(buf, 0, bytesRead); } out.closeEntry(); } private void replace(String toReplace, String replacement) throws IOException { Path source = Paths.get(replacement); Path target = Paths.get(toReplace); Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); } }



Other than that the explanation in the link you found about updating jars was pretty good. I couldn't have done it better. What is it you need now?

Well i have got a few ideas about it and I think it should work. Also the explanation is helpful.

Suppose that I make a tool and create an exe of that using install4j or Java2exe etc. then it would create an installer for me but still the main and core library would be the original one which will be placed at some place and hence, when I check for updates I close the original window and a different instance of update manager would start and I will grab the latest jar and replace it in its position. This is a simple idea and should work, according to your reply and the links we got.

But still while designing the tool care has to be taken and the design has to be proper so that working is smooth.


I saw your code, and whenever I see your codes, it has a soothing effect in my eyes. So beautiful. Artistically carved out a little piece of Code Biggrin
[Image: OilyCostlyEwe.gif]

Reply

RE: Update Feature in a Java Tool #6
Why would you use a tool like java2exe in the first place?
If you bundle your jar within the exe you could probably make a hack, though. A lot of these tools just append the jar to the end of the file and if you don't use any protection mechanisms, the jar is readable as is. So you could write an updater that replaces that part of the file and it should still work. The updater can find the zip in the file by searching for a valid zip signature and trying to read the zip entries at that point. If reading works, it is the jar.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Update Feature in a Java Tool #7
(03-27-2014, 08:42 AM)Deque Wrote: Why would you use a tool like java2exe in the first place?
If you bundle your jar within the exe you could probably make a hack, though. A lot of these tools just append the jar to the end of the file and if you don't use any protection mechanisms, the jar is readable as is. So you could write an updater that replaces that part of the file and it should still work. The updater can find the zip in the file by searching for a valid zip signature and trying to read the zip entries at that point. If reading works, it is the jar.

I wanted to use that for making an installer, like many applications do.
exe in for windows, for linux diastros I would create deb or rpm packages (I have read about how do do this already).

Well what do you suggest then, instead of using those tools, I should leave the executable jar as it is and then run an update. Thereby replace the jar itself with the updated one.
[Image: OilyCostlyEwe.gif]

Reply

RE: Update Feature in a Java Tool #8
Why do you need an installer? Does your tool need an installer?
Java's advantage is that it runs out of the box.
I can only imagine that a user might not have the JVM installed. But that's rare.
Of course packages for linux distros might be beneficial.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Update Feature in a Java Tool #9
(03-27-2014, 09:18 AM)Deque Wrote: Why do you need an installer? Does your tool need an installer?
Java's advantage is that it runs out of the box.
I can only imagine that a user might not have the JVM installed. But that's rare.
Of course packages for linux distros might be beneficial.

Installer is just for a bit fun. It looks good, Something installing and then you use it. Biggrin

Yeah I guess then its final. Say no to installers. I will do that, Check for update and then replace the new updated jar with the existing one.
[Image: OilyCostlyEwe.gif]

Reply