Eclipse插件-如何运行外部类

Eclipse插件-如何运行外部类,eclipse,plugins,Eclipse,Plugins,我想为Eclipse制作一个插件。问题是,我查看了API和示例,并在主栏上创建了一个按钮,带有一个特定的图标,当我单击它时,会打开一个InputDialog 困难的部分是,我想从这个按钮开始应用程序,但不是运行时,因为它是一个新的进程。我只想在插件中启动一个类,它将登录到服务器并从中获得一些输出。我希望它在控制台中打开,就像启动普通应用程序一样,或者在单独的控制台中打开 最好的例子是Tomcat插件,它启动Tomcat,然后将控制台输出到Eclipse控制台。我也想这么做。我看过Tomcat源代

我想为Eclipse制作一个插件。问题是,我查看了API和示例,并在主栏上创建了一个按钮,带有一个特定的图标,当我单击它时,会打开一个InputDialog

困难的部分是,我想从这个按钮开始应用程序,但不是运行时,因为它是一个新的进程。我只想在插件中启动一个类,它将登录到服务器并从中获得一些输出。我希望它在控制台中打开,就像启动普通应用程序一样,或者在单独的控制台中打开


最好的例子是Tomcat插件,它启动Tomcat,然后将控制台输出到Eclipse控制台。我也想这么做。我看过Tomcat源代码插件,但我也被困在那里了。他们使用自己的启动器。

我不知道你所说的“我只想开始一门课”是什么意思。我假设您想要执行一个命令行工具,并将其输出重定向到控制台窗口

要做到这一点而不产生新的进程,您必须能够控制工具的输出流。如果无法控制,则您别无选择,只能启动新流程以正确捕获工具的输出

从技术上讲,可以调用
System.setOut
,但它会将所有线程的输出重定向到控制台,这不是您想要的

不过,您首先要创建一个控制台:

//函数findConsole复制自:
// http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
private MessageConsole findConsole(字符串名称){
ConsolePlugin plugin=ConsolePlugin.getDefault();
IConsoleManager conMan=plugin.getConsoleManager();
IConsole[]existing=conMan.getConsoles();
for(int i=0;i
然后设置我的工具的输入和输出流,并在不同的线程中开始处理,这样UI就不会阻塞

//创建我的工具并重定向其输出
最终MyTool MyTool=新的MyTool();
myTool.setOutputStream(输出);
myTool.setErrorStream(err);
myTool.setInputStream(in);
//从另一个线程开始
线程t=新线程(新的可运行线程(){
公开募捐{
myTool.startExecuting();
}
});
t、 start();
如果您的工具不支持I/O重定向,您别无选择,只能在另一个进程中使用
ProcessBuilder
启动它,并使用多个线程在控制台和进程流之间移动数据。请参见:
process.getInputStream()
process.getOutputStream()
process.getErrorStream()

以下链接提供了其他有用的详细信息:


这是运行新控制台的代码,该控制台具有停止删除和删除全部等控件!这是我在开始时要求的,但是消息控制台很好知道

ILaunchConfigurationType launchType = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType("org.eclipse.jdt.launching.localJavaApplication");
ILaunchConfigurationWorkingCopy config = null;
try {
    config = launchType.newInstance(null, "My Plugin working");
} catch (CoreException e) {
    System.err.println(e.getMessage());
}
config.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, "org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector");
String[] classpath = new String[] { "C:\\Users\\Administrator\\Documents\\myjr.jar" };
ArrayList classpathMementos = new ArrayList();
for (int i = 0; i < classpath.length; i++) {
    IRuntimeClasspathEntry cpEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(classpath[i]));
    cpEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
    try {
        classpathMementos.add(cpEntry.getMemento());
    } catch (CoreException e) {
        System.err.println(e.getMessage());
    }
}
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpathMementos);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "collectorlog.handlers.MyClass");
try {
    ILAUNCH = config.launch(ILaunchManager.RUN_MODE, null);
} catch (CoreException e) {
    System.err.println(e.getMessage());
}
ILaunchConfigurationType launchType=DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(“org.eclipse.jdt.launching.localJavaApplication”);
ILaunchConfigurationWorkingCopy配置=null;
试一试{
config=launchType.newInstance(null,“我的插件正在工作”);
}捕获(COREE){
System.err.println(e.getMessage());
}
config.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID,“org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector”);
String[]classpath=新字符串[]{“C:\\Users\\Administrator\\Documents\\myjr.jar”};
ArrayList classpathMementos=新的ArrayList();
for(int i=0;i