Hello,

I am trying to create a update section of a game but running into a problem of JFrames.

What I want to do is have a single JFrame that will have the update script, then after the update it will start a jar file in the JFrame (With the applied updates).

Is there a way to set the contents of a JFrame to a jar file?

An example of this is (i think) a game called Minecraft (By Notch) where you launch the application, you login and it updates (if needed). After all of that the minecraft.jar file (the one downloaded by the update script) is started in the JFrame without closing it.


Any and all help is appreciated.

-- Turt2Live

Dani AI

Generated

You cannot “set a JFrame to a jar file.” A JAR is just an archive of classes and resources. To keep one launcher window open while running updated code you have two practical options: load the updated JAR into your running JVM and instantiate its UI classes inside the existing JFrame, or launch the updated JAR in a separate JVM process and keep the launcher window as a monitor. was on the right track about using classes from a JAR; here are concrete, practical ways to do what described.

For an in-process solution (keeps everything inside the same JFrame): define a small, stable interface in the launcher (for example, a provider that returns a JPanel). Build the downloadable JAR to implement that interface but do not repackage the interface class into the game JAR. At runtime create a new URLClassLoader pointing at the downloaded JAR, load the provider class by name, instantiate it via reflection, get its JPanel, then swap that panel into the launcher frame on the Swing EDT. When updating, remove the old panel, stop any threads the module started, call URLClassLoader.close() (Java 7+), drop references, and let the classloader be GCed to avoid memory leaks.

If isolation is preferable: spawn a new JVM with ProcessBuilder (java -jar files/MyJar.jar). This is simpler and avoids classloader/static leaks and lets you tune memory flags, but you cannot reliably embed that other JVM’s window into your launcher across platforms — usually the launcher hides or stays visible to show progress/logs while the game runs in its own window.

Eclipse/runtime notes: for development add the game JAR to the project build path via Project → Properties → Java Build Path → Libraries → Add JARs / Add External JARs. For a runtime-only dynamic loader you do not need the JAR on the launcher’s compile-time classpath. Troubleshooting checklist: ensure UI work happens on the EDT, avoid duplicating shared interfaces in both JARs, stop background threads before unloading, and test close()/nulling of classloaders to prevent leaks.

Recommended Answers

All 6 Replies

Is there a way to set the contents of a JFrame to a jar file?

A jar file contains files. Just about any type of file can put put in a jar file.
In that context, what do you mean by the above?

By putting the jar file on the classpath when running your program, you can call/use any classes that are in the jar file. For example if there were a class that extended JPanel, you could add that to your JFrame.

So a jar file is just like a zip file (theoretically)?

If the classpath includes the jar file, then would I call the class as normal?
eg: JarClass jar = new JarClass();

or something else?

As well, would the jar need to be executable?

Thanks for your help.

Yes the jar file is like a zip file.
yes, if the JarClass class is in the jar file, that new statement should work, if you have the package name correct.

Not sure what "need to be executable" means in this context. For the above usage, it would not need to be executable.

By executable I assume you mean:
java -jar JarFileName.jar
would start a program from within the jar file.

Yes, thats what I meant.

One last question, in Eclipse, how would you add that second jar (JarClass) to the class path? How would that change if the jar can be placed anywhere in the computer (As I have it setup right now it will dump all required files to a directory in the same directory the program was started in, so the jar could be in C:\Users\Me\MyProgram or C:\MyProgram although the jar file will always be in the directory "files" so C:\MyProgram\files\MyJar.jar or C:\Users\Me\MyProgram\files\MyJar.jar)?

Sorry, I don't know how to use Eclipse.

Ok, thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.