简体   繁体   English

启动外部JAR文件需要什么,比如Minecraft启动程序?

[英]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. 如果你从未玩过Minecraft,那么这就是发射器的机械工作方式。

The user can download a JAR (Or a JAR packaged into an EXE), which has absolutely no code for the Minecraft client at all. 用户可以下载一个JAR(或者一个打包到EXE中的JAR),它完全没有Minecraft客户端的代码。 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. 当启动器启动时,它会显示一个带有新闻等的登录屏幕。然后,登录后,启动器将运行主要的Minecraft核心,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. Minecraft启动程序不需要任何外部Java库来运行。

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) 我正在尝试复制这个功能,但是,当我在Eclipse中导出为可运行的JAR时,当我尝试启动它时,它会输出“找不到主类launcher.jar。程序将退出”(这是在控制台上) ,我希望能够双击JAR并让它启动)

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();

Runnable JARs need a Manifest that indicates the main class to use. Runnable JAR需要一个Manifest来指示要使用的主类。 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. 从eclipse导出时,一个选项是选择运行配置(例如,从之前的测试运行),eclipse使用该信息填充清单。

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. 更新:在考虑了它之后,基于上面的输出和下面的注释,我的猜测是你试图使用命令行中的'java'命令运行jar。 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 C:\\ Users \\ Tim \\ Desktop> java launcher.jar
Exception in thread "main" java.lang.NoClassDefFoundError: launcher/jar Caused by: 线程“main”中的异常java.lang.NoClassDefFoundError:launcher / jar引起:
java.lang.ClassNotFoundException: launcher.jar java.lang.ClassNotFoundException:launcher.jar
at java.net.URLClassLoader$1.run(Unknown Source) 在java.net.URLClassLoader $ 1.run(未知来源)
at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source) at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: launcher.jar. 找不到主类:launcher.jar。 Program will exit. 程序将会退出。

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

// 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());

您应该尝试使用Java Decompiler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM