How do I dynamically load the Java3D library?

为君一笑 提交于 2019-12-11 04:55:22

问题


I am finishing up a game that I developed using 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. 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. Is there a better solution to this? Or is dynamically loading the Jars the way to go?

I have installed Java3D on another computer of mine from here:

  • Download 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.

Are there any more efficient solutions to this? Is dynamically loading the Jars just the way to go?


EDIT

Here is my problem simplified:

  1. Java3D is not a standard part of Java, it is an extension that must be installed separately
  2. Java3D consists of 3 Jar Files (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

Now here is my simplified question:

How do I run Java3D on a computer that doesn't have Java3D installed? 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. How can I do this?


回答1:


The jars are not platform specific, the native code in the dynamic libraries is. 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. There is a reason for this, JNI (the Java Native Interface) looks for certain extensions based on what system it is running on. 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.

Some code to point you in the right direction:

Blah.java

public class blah{
    public native void meh();

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

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.java is compiled into a .class file, toss it in a jar just for ease for execution. 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 finds the files because they are in the PATH environment variable (or the system equivalent). 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.

Basically you just need to worry about the part about the PATH as everything else is done for you!




回答2:


If you are distributing it over disk, you can wrap your application in a setup executable (for instance using the free 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.

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.




回答3:


Download the classes from here: http://java3d.java.net/binary-builds.html and extract the included zip file. Extract the jar files (vecmath, j3dcore, j3dutils) in the lib folder to your class folder. Bundle your class folder to a single jar file.

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?



来源:https://stackoverflow.com/questions/11838393/how-do-i-dynamically-load-the-java3d-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!