Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is needed to launch external JAR files, like the Minecraft launcher?

If you have never played Minecraft, then this is how the mechanics of the launcher work.

The user can download a JAR (Or a JAR packaged into an EXE), which has absolutely no code for the Minecraft client at all. This is deemed the launcher. When the launcher is started, it displays a login screen with news etc. Then, after logging in, the launcher then runs the main Minecraft core, minecraft.jar. If it is not present on the system, it downloads it. The Minecraft launcher doesn't need any external Java libraries to run either.

How does it do this?

I'm currently trying to replicate the functionality, however, when I export as a runnable JAR in Eclipse, when I try launch it, it prints "Could not find main class launcher.jar. Program will exit" (this is on the console, I want to be able to double click the JAR and have it launch)

File file = new File(System.getProperty("user.dir") + "/lessur.jar"); 
System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "natives");
URLClassLoader classLoader;
classLoader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()});          
classLoader.loadClass("zombie.engine.Lighting2").newInstance();
like image 567
liamzebedee Avatar asked Oct 10 '22 12:10

liamzebedee


2 Answers

Runnable JARs need a Manifest that indicates the main class to use. While exporting from eclipse, an option is to select a run configuration (from a previous test run for instance) and eclipse uses that info to populate the manifest.

Update: After thinking about it a little more, based on the output above and comments below, my guess is that you are trying to run the jar using the 'java' command from the command line. If that is the case, you need to use:
java -jar launcher.jar
not:
java launcher.jar

Here is a reproduction of what I think you see:

C:\Users\Tim\Desktop>java launcher.jar
Exception in thread "main" java.lang.NoClassDefFoundError: launcher/jar Caused by:
java.lang.ClassNotFoundException: launcher.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: launcher.jar. Program will exit.

C:\Users\Tim\Desktop>java -jar launcher.jar
Ran

like image 182
Tim Bender Avatar answered Oct 13 '22 10:10

Tim Bender


// Use the File constructor that will insert the correct separator for the OS
File file = new File(System.getProperty("user.dir"), "lessur.jar"); 
System.out.println( "File exists: " + file.exists() );
File libs = new File(System.getProperty("user.dir"), "natives");
System.out.println( "Libs exists: " + libs.exists() );
System.setProperty("org.lwjgl.librarypath", libs.toString());
like image 22
Andrew Thompson Avatar answered Oct 13 '22 09:10

Andrew Thompson