2

我有以下代码:

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();
        }
    }}

实际上,我只想运行绝对路径定义的文件中指定的启动配置。我收到一个 NPE,因为 DebugPlugin.getDefault() 返回 null。我该怎么办?我发现了很多类似的例子,但没有一个是关于 NPE 的,就像在我之前没有人得到它一样。

4

1 回答 1

1

快速查看 DebugPlugin代码后,我发现该DebugPlugin#getDefault()方法是简单的 getter 到fgDefaultPlugin字段,并返回默认情况下的此字段值null。由于您正在调用函数的DebugPlugin#getDefault()第一个方法,main因此它返回是合理的null,因为DebugPlugin#setDefault()以前没有调用过。

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.

于 2013-01-29T20:23:16.493 回答