3

I have a jar file that has been turned into a .exe using L4J, and another jar file in appdata. The reason for having two files is that I need an updating mechanism.

My question: How do I run the .exe file on the desktop, then load the jar in appdata from it?

Mad3ngineer
  • 113
  • 2
  • 11
  • Use [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) to install the app. Auto-updating comes built in for free, and it works on Windows, OS X & *nix. – Andrew Thompson Sep 20 '12 at 23:12
  • I have looked at web-start. I think that something I make myself could be more proffesional. I want my own GUI for the updater/launcher. – Mad3ngineer Sep 21 '12 at 14:30
  • 1
    Ah. Typos. I will add that in any questions that I ask about this. I'm sorry for not including that, I didn't think that was within the scope of the question. I'll provide more info from now on. – Mad3ngineer Sep 22 '12 at 20:34

2 Answers2

3

You could use a URLClassLoader to load the second Jar at runtime.

Depending on your needs, you may need a bridging interface (one that exists in both Jars) that you would call from your 'exe' to get the second Jar running...or you could simply use the second Jar's main method ;)

The other choice you have is to run another JVM.

UPDATE

In order to physical seperate the two elements of your application. You have a Jar wrapped in a EXE (aka launcher) and another Jar which is your application (aka application) (I assume).

So. Your launcher should have absolutely no idea about your application (little to no compile time dependencies).

Some how, we need to dynamically load the application from the launcher. To do that, we need a few things.

We need to be able to load the application into the launchers class loader context (so we can see it) and we some we to be able to load the application.

Dynamic ClassLoading

This can be achieved simply through the use of URLClassLoader

URLClassLoader loader = new URLClassLoader(new URL[]{new File("path/to/your/jar/Application.jar").toURI().toURL()});

Application Loading

This can be achieved in one of two ways. You could simply use the URLClassLoader to find a launch the applications main class...

// This is essentially the same as saying 
// the.package.name.to.you.main.class.Main.main(new String[]{});
Class<?> mainClass = loader.loadClass("the.package.name.to.you.main.class.Main");
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.invoke(null, new String[]{});

Now, if your application Jar doesn't have a main method, you can use the above example to launch just about any class you want...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

you need to add a jar, by at to classpath, for eg: "c:\mypath\myjar.jar" than you will update that myjar.jar

  • So you add the classpath to the metadata inside the .exe wrapped jar, Then run the main class from the .exe the way you would load a library, right? – Mad3ngineer Sep 20 '12 at 22:11
  • yes, and after each re-launch the new jar is there, like the .dll files in windows :) –  Sep 20 '12 at 22:13
  • Beware, if your launcher (exe) loads the application (Jar) you will not be able to write over the jar, as Java's classloaders keep the Jar's open. – MadProgrammer Sep 20 '12 at 22:59
  • yes it is true, can be manually rename that file or launch another utility to do it that file rename, when the orig jar exit –  Sep 20 '12 at 23:02
  • Yes, the .exe will update the jar before it launches or imports the jar. Thanks! – Mad3ngineer Sep 21 '12 at 14:31