如何动态加载Java3D库? [英] How do I dynamically load the Java3D library?

查看:126
本文介绍了如何动态加载Java3D库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了一个使用Java3D开发的游戏。由于Java3D不符合标准JRE,所以我给这个游戏的所有人都必须下载Java3D。我的一些朋友少了一点计算机的智慧,所以我认为把这个JAR写入磁盘并用Java动态加载的工作要少得多。有更好的解决方案吗?或者是动态加载Jars的方式吗?



我已经在另一台计算机上安装了Java3D:





相当快速的安装,但如果您根本不需要下载任何东西,那将会更好。如果他们只是在使用Java3D进行游戏,那么他们遇到下载麻烦似乎并不合乎逻辑。



有没有更有效率这个解决方案?是动态加载Jars的方式吗?






编辑



这是我的问题简化:


  1. Java3D不是Java的标准部分,扩展名必须单独安装

  2. Java3D包含3个Jar文件(j3core.jar,j3dutils,vecmath)

  3. 我想要能够运行我的游戏在其他电脑上没有他们下载的jar文件

现在这里是我的简化问题:



如何在没有安装Java3D的计算机上运行Java3D?我想将Jar文件与游戏一起写入CD,并且能够直接从CD播放游戏,无需安装任何下载。如何做到这一点?

解决方案

这些jar不是特定的平台,动态库中的本机代码是。动态库已经加载,因为它们在代码中被调用。如果您回顾该代码,您将看到 loadLibrary()不要求扩展。有一个原因,JNI(Java Native Interface)根据运行的系统寻找某些扩展。因此,只需打包JNI可以找到的所有动态库,例如在应用程序旁边,JNI将保留其余的。



有些代码要点你在正确的方向:



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>
#includeBlah.h

JNIEXPORT void JNICALL Java_Blah_meh(){
cout<<< 咩。;
}

Blah.cpp(由javah程序生成的Blah.h)是编译成每个主要系统的动态库(thing.dll,thing.dylib,thing.so)。 Blah.java被编译成.class文件,将其放在jar中,以方便执行。
现在的重要部分,文件的布局!

  ThingApp //这是一个文件夹
| thing.dll
| thing.dylib
| thing.so
| Blah.jar

将这个放在任何系统上,由于扩展,JNI将为系统找到正确的文件,将库加载到内存中,执行代码,并打印出Meh。 JNI找到文件,因为它们位于PATH环境变量(或系统等效项)中。 PATH包括执行的代码/程序所在的文件夹,使用 java.library.path 系统属性设置的文件夹,并且包含的​​文件夹包含PATH变量本身。 p>

基本上,您只需要担心PATH的一部分,因为其他一切都为您完成!


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:

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?

解决方案

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!

这篇关于如何动态加载Java3D库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆