Pregunta

I've got the following piece of code:

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;


public class Main {
    public static void main(String... strings) throws IOException {
        try {
            String path = "E:\\Java\\Projects\\.metadata\\.plugins\\org.eclipse.debug.core\\.launches\\MedicineFrame.launch";

            ILaunchManager launchManager = DebugPlugin.getDefault()
                    .getLaunchManager();
            ILaunchConfigurationType type = launchManager
                    .getLaunchConfigurationType(ILaunchManager.RUN_MODE);
            ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(
                    null, path);
            workingCopy.setAttribute("PATH_MY", path);
            ILaunchConfiguration configuration = workingCopy.doSave();
            DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }}

Actually, I just want to run the launch configuration specified in the file defined by absolute path. I receive an NPE, because the DebugPlugin.getDefault() returns null. What should I do? I've found a lot of similar examples, but none of them tells about the NPE, like no one has ever got it before me.

¿Fue útil?

Solución

After quick look at DebugPlugin code I found that DebugPlugin#getDefault() method is simple getter to fgDefaultPlugin field, and return this field value which is null by default. Since you're invoking DebugPlugin#getDefault() as very first method of your main function it's reasonable that it returns null, since DebugPlugin#setDefault() was not previously called.

You can't run Eclipse from a main method like that. You need to write a plugin, and it will be accesible from inside the plugin method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top