我有以下代码:
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 的,就像在我之前没有人得到过它一样。
最佳答案
快速浏览 DebugPlugin 后 code我发现 DebugPlugin#getDefault()
方法是对 fgDefaultPlugin
字段的简单获取,并返回默认为 null
的字段值。由于您正在调用 DebugPlugin#getDefault()
作为 main
函数的第一个方法,因此它返回 null
是合理的,因为 DebugPlugin#setDefault()
之前未被调用。
您不能从这样的 main 方法运行 Eclipse。您需要编写一个插件,并且可以从插件方法内部访问它。
关于java - DebugPlugin.getDefault() 中的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14591339/