简体   繁体   English

如何动态加载Java3D库?

[英]How do I dynamically load the Java3D library?

I am finishing up a game that I developed using Java3D. 我正在完成一个使用Java3D开发的游戏。 Since Java3D does not come with the standard JRE, all of the people who I am giving this game to will have to download Java3D. 由于Java3D没有标准的JRE,所以我给这个游戏的所有人都必须下载Java3D。 Some of my friends are a little less computer savvy so I thought it would be a lot less work to just write the Jars to the disk and dynamically load them in Java. 我的一些朋友对计算机的了解程度要低一些,所以我认为将Jars写入磁盘并用Java动态加载它们的工作要少得多。 Is there a better solution to this? 有更好的解决方案吗? Or is dynamically loading the Jars the way to go? 或者是动态加载Jars的方式去?

I have installed Java3D on another computer of mine from here: 我从这里在另一台计算机上安装了Java3D:

It was a pretty quick installation but it would be better if you didn't have to download anything at all. 这是一个非常快速的安装,但如果您不必下载任何东西会更好。 If they are only going to use Java3D for my game then it doesn't really seem logical for them to go through the trouble of downloading it. 如果他们只是在我的游戏中使用Java3D,那么他们在下载它时遇到麻烦似乎并不合乎逻辑。

Are there any more efficient solutions to this? 有没有更有效的解决方案呢? Is dynamically loading the Jars just the way to go? 是动态加载Jars的方式吗?


EDIT 编辑

Here is my problem simplified: 这是我的问题简化:

  1. Java3D is not a standard part of Java, it is an extension that must be installed separately Java3D不是Java的标准部分,它是必须单独安装的扩展
  2. Java3D consists of 3 Jar Files (j3core.jar, j3dutils, vecmath) Java3D由3个Jar文件组成(j3core.jar,j3dutils,vecmath)
  3. I want to be able to run my game on other computers without them having to download the Jar files 我希望能够在其他计算机上运行我的游戏而无需下载Jar文件

Now here is my simplified question: 现在这是我的简化问题:

How do I run Java3D on a computer that doesn't have Java3D installed? 如何在未安装Java3D的计算机上运行Java3D? I want to write the Jar files to a CD, along with the game, and be able to play the game right off the CD, no installations no downloads. 我想将Jar文件与游戏一起写入CD,并且能够直接在CD上播放游戏,没有安装没有下载。 How can I do this? 我怎样才能做到这一点?

If you are distributing it over disk, you can wrap your application in a setup executable (for instance using the free Inno Setup ). 如果您通过磁盘分发它,则可以将应用程序包装在设置可执行文件中(例如使用免费的Inno Setup )。 Once the users put in your disk, the setup will start. 用户放入磁盘后,将开始安装。 You can configure your setup in Inno Setup to also install the correct version of Java3D. 您可以在Inno Setup中配置您的设置,以便安装正确版本的Java3D。

I think that is how most of the disk-distributed games work. 我认为这就是大多数磁盘分布式游戏的工作原理。 It takes a bit to get the hang of those more advanced setup wrappers, but they are worth it. 要获得那些更高级的设置包装器,需要一点点,但它们是值得的。 I don't know anything else than Inno Setup, but it was able to handle any of my requirements so far that's why I recommend that one. 除了Inno Setup之外我什么都不知道,但到目前为止我能够处理我的任何要求,这就是为什么我推荐那个。

The jars are not platform specific, the native code in the dynamic libraries is. 这些jar不是特定于平台的,动态库中的本机代码是。 The dynamic libraries are already loaded as they are called for in code. 动态库已在代码中调用时加载。 If you take a look back at that code you will see that loadLibrary() does not ask for the extension. 如果您回顾一下该代码,您会看到loadLibrary()不会要求扩展。 There is a reason for this, JNI (the Java Native Interface) looks for certain extensions based on what system it is running on. 这是有原因的,JNI(Java Native Interface)根据它运行的系统寻找某些扩展。 For this reason just package all the dynamic libraries where JNI can find it, such as right next to your application and JNI will take care of the rest. 因此,只需将JNI可以找到的所有动态库打包,例如在应用程序旁边,JNI将负责其余的工作。

Some code to point you in the right direction: 一些代码指向正确的方向:

Blah.java Blah.java

public class blah{
    public native void meh();

    public static void main(String args[]) {
        meh();
    }
}
static {
    System.loadLibrary("thing");
}

Blah.cpp Blah.cpp

#include <jni.h>
#include <iostream>
#include "Blah.h"

JNIEXPORT void JNICALL Java_Blah_meh() {
    cout << "Meh.";
}

Blah.cpp (with the Blah.h generated by the javah program) is compiled into a dynamic library for each major system (thing.dll, thing.dylib, thing.so). Blah.cpp(由javah程序生成的Blah.h)被编译成每个主要系统的动态库(thing.dll,thing.dylib,thing.so)。 Blah.java is compiled into a .class file, toss it in a jar just for ease for execution. Blah.java被编译成一个.class文件,只是为了便于执行而将其丢弃在一个jar中。 Now for the important part, the layout of files! 现在重要的是,文件的布局!

ThingApp //that's a folder
  |thing.dll
  |thing.dylib
  |thing.so
  |Blah.jar

Put this on any system and JNI will find the right file for the system due to the extensions, load the library into memory, execute the code, and print out "Meh.". 将它放在任何系统上,由于扩展,JNI将为系统找到正确的文件,将库加载到内存中,执行代码并打印出“Meh。”。 JNI finds the files because they are in the PATH environment variable (or the system equivalent). JNI查找文件,因为它们位于PATH环境变量(或系统等效项)中。 The PATH includes the folder the executed code/program is in, folders set with the java.library.path system property, and the folders included int he PATH variable itself. PATH包括执行的代码/程序所在的文件夹,使用java.library.path系统属性设置的文件夹,以及包含在PATH变量本身中的文件夹。

Basically you just need to worry about the part about the PATH as everything else is done for you! 基本上你只需要担心PATH的部分,因为其他一切都是为你完成的!

Download the classes from here: http://java3d.java.net/binary-builds.html and extract the included zip file. 从这里下载类: http//java3d.java.net/binary-builds.html并解压缩包含的zip文件。 Extract the jar files (vecmath, j3dcore, j3dutils) in the lib folder to your class folder. 将lib文件夹中的jar文件(vecmath,j3dcore,j3dutils)解压缩到类文件夹。 Bundle your class folder to a single jar file. 将类文件夹捆绑到单个jar文件中。

Now follow these steps to add the libraries (*.dll, *.so) from the bin folder in the above zip file to your jar file: How to make a JAR file that includes DLL files? 现在按照以下步骤将上面zip文件中bin文件夹中的库(* .dll,* .so)添加到jar文件中: 如何制作包含DLL文件的JAR文件?

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

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