5

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();
Peter O.
  • 32,158
  • 14
  • 82
  • 96
liamzebedee
  • 14,010
  • 21
  • 72
  • 118

3 Answers3

3

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

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • I've done this, here is the contents of MANIFEST.MF from the exported JAR - Manifest-Version: 1.0 Class-Path: . Main-Class: Main – liamzebedee Nov 12 '11 at 10:23
  • Presumably you have a class called `Main.class` (with no package) in that JAR and that class contains a properly formatted `main` function? – Tim Bender Nov 12 '11 at 19:46
  • Okay, are you double-clicking the launcher.jar file or trying to run it with the "java" command line? My guess is the latter of the two. I've updated my answer. – Tim Bender Nov 13 '11 at 03:49
  • I'm double clicking it, which doesn't produce any output, from which I then debug what happened, by launching it via the command line. – liamzebedee Nov 13 '11 at 04:41
  • Do you launch it with the `-jar` option as I mentioned? `java -jar launcher.jar` – Tim Bender Nov 13 '11 at 05:26
1
// 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());
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

You should try a Java Decompiler