LG3D の起動

とりあえず、LG3D は動くようになりました。でも、どうして動いているのかはまだよく分かってなかったりするのですが ^^;;;;;;; まぁ、いいや。

クラスパス、ブートクラスパス、JVM へのオプション、起動するクラスなどすべて決めうちです。

lg3d-dev もしくは lg3d-app などでは RMI を使う必要があるのですが、今回は RMI を使わないようにするため、起動クラスは org.jdesktop.lg3d.displayserver.Main ではなくて、サンプルで使った Monolith1 クラスにしてあります。

結局、LG3DLauncher クラスは次のようになっています。

package jp.gr.java_conf.skrb.lg3d.wrpe;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jdt.launching.ExecutionArguments;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMRunner;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.LibraryLocation;
import org.eclipse.jdt.launching.VMRunnerConfiguration;

public class LG3DLauncher {
    private String classpath[] = {
            "D:/lg3d/",
            "D:/lg3d/lib/ext/lg3d-core.jar",
            "D:/lg3d/ext/activation.jar",
            "D:/lg3d/ext/escher-0.2.2.lg.jar",
            "D:/lg3d/ext/j3d-contrib-utils.jar",
            "D:/lg3d/ext/jaimlib.jar",
            "D:/lg3d/ext/mail.jar",
            "D:/lg3d/ext/nwn-0.7.jar",
            "D:/lg3d/ext/odejava.jar",
            "D:/lg3d/ext/satin-v2.3.jar"};

    private String vmArgs = "-Xmx512m -Dj3d.sortShape3DBounds=\"true\""
                            + " -Dlg.configurl=file:///D:/lg3d/etc/lg3d/lgconfig_1p_nox.xml"
                            + " -Dlg.displayconfigurl=file:///D:/lg3d/etc/lg3d/displayconfig/j3d1x1"
                            + " -Dlg.etcdir=D:\\lg3d\\etc\\";
    private String prgArgs = "";
    private String classToLaunch = "Monolith1";
    
    public void launch() throws CoreException {
        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
        IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
        
        ILaunchConfigurationWorkingCopy config = createConfig(vmInstall, false);
        
        Launch launch = new Launch(config, ILaunchManager.RUN_MODE, null);

        VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(classToLaunch, classpath);
        ExecutionArguments executionArguments = new ExecutionArguments(vmArgs, prgArgs);
        vmConfig.setVMArguments(executionArguments.getVMArgumentsArray());
        vmConfig.setProgramArguments(executionArguments.getProgramArgumentsArray());
        vmConfig.setBootClassPath(null);   

        vmRunner.run(vmConfig, launch, null);
    }
    
    public ILaunchConfigurationWorkingCopy createConfig(IVMInstall vmInstall, boolean saveConfig)
                                                                            throws CoreException {

        ILaunchConfigurationType launchType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType("org.eclipse.jdt.launching.localJavaApplication");
        ILaunchConfigurationWorkingCopy config = launchType.newInstance(null, "LG3D");
        config.setAttribute(IDebugUIConstants.ATTR_PRIVATE, !saveConfig);
        DebugUITools.setLaunchPerspective(launchType, "debug",
                                          "Perspective to switch when LG3D is started");
        DebugUITools.setLaunchPerspective(launchType, "run",
                                          "Perspective to switch when LG3D is started");

        config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, classToLaunch);

        List<String> classpathMementos = new ArrayList<String>();
        for (int i = 0; i < classpath.length; i++) {
            IRuntimeClasspathEntry cpEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(classpath[i]));
            cpEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
            classpathMementos.add(cpEntry.getMemento());
        }

        LibraryLocation[] librairies = vmInstall.getVMInstallType().getDefaultLibraryLocations(vmInstall.getInstallLocation());
        for (int i = 0; i < librairies.length; i++) {
            IRuntimeClasspathEntry cpEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(librairies[i].getSystemLibraryPath());
            cpEntry.setClasspathProperty(IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
            classpathMementos.add(cpEntry.getMemento());
        }

        config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
        config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpathMementos);
        config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, prgArgs);
        config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs);

        return config;
    }
}

よく分かっていないのが、ILaunchConfigurationWorkingCopy オブジェクトなどにプロパティを設定していますが、どれが必須でどれが省略していいのか、またプロパティの意味も分かっていないところが多々。

そのうち分かるようになるでしょう。と、こういうのばっかですが。

実行すると...

WRPE 実行結果

次は、決めうちにしているところを Eclipse の環境から読みこんでということはしなくて、先に LG3D を終了できるようにしてみます。