A API do JDT inclui recursos para lançar um programa Java utilizando a instalação da VM que está atualmente configurada pelo usuário de um projeto Java.
Os programas Java que foram compilados em um projeto Java podem ser executados obtendo o IVMRunner apropriado do projeto Java e executando a classe pelo nome. O trecho de código a seguir mostra como a classe MyClass dentro de myJavaProject pode ser lançada.
IVMInstall vmInstall = JavaRuntime.getVMInstall(myJavaProject);
if (vmInstall == null)
vmInstall = JavaRuntime.getDefaultVMInstall();
if (vmInstall != null) {
IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
if (vmRunner != null) {
String[] classPath = null;
try {
classPath = JavaRuntime.computeDefaultRuntimeClassPath(myJavaProject);
} catch (CoreException e) { }
if (classPath != null) {
VMRunnerConfiguration vmConfig =
new VMRunnerConfiguration("MyClass", classPath);
ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
vmRunner.run(vmConfig, launch, null);
}
}
}
Outra maneira de lançar um programa Java é criar uma configuração de lançamento de aplicativo Java e lançá-la. O trecho a seguir mostra como a classe MyClass dentro de myJavaProject pode ser lançada utilizando uma configuração de lançamento simples. Por padrão, o aplicativo em execução resultante utiliza o JRE e o classpath associado a myJavaProject.
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy wc = type.newInstance(null, "SampleConfig");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "myJavaProject");
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "myClass");
ILaunchConfiguration config = wc.doSave();
config.launch(ILaunchManager.RUN_MODE, null);