Eclipse代码launch指定MainClass

本文介绍了如何在Eclipse中通过代码实现运行或调试特定的Java主类(MainClass)。提供了一个名为`LaunchMainClass`的Java类,该类包含了查找或创建launch configuration并启动debug模式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用代码run或者debug某个test.java的main方法


package com.tmall.testgen.launch;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
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.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;

/**
 *
 * @author liuzx
 *
 */
public class LaunchMainClass {
    public static void test() {
        String projectName = "testgen";
        String mainClass = "com.test.Test";
        launch(projectName, mainClass);
    }

    public static void launch(String projectName, String mainClass) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IProject project = workspace.getRoot().getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);
        String name = mainClass;
        try {
            launch(javaProject, name, mainClass);
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }

    private static void launch(IJavaProject proj, String name, String mainClass)
            throws CoreException {
        try {
            ILaunchManager manager = DebugPlugin.getDefault()
                    .getLaunchManager();

            //java application配置类型
            ILaunchConfigurationType type = manager
                    .getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);

            ILaunchConfiguration config = null;
            ILaunchConfiguration[] configurations = manager
                    .getLaunchConfigurations(type);

           //查看 是否已经存在
            for (int i = 0; i < configurations.length; i++) {
                if (configurations[i].getName().equals(name))
                    config = configurations[i];
            }
            // else create a new one
            if (config == null) {
                ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
                        name);
                wc.setAttribute(
                        IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                        proj.getProject().getName());
                // current directory should be the project root
                wc.setAttribute(
                        IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
                        proj.getProject().getLocation().toString());
                // use the suplied args
                wc.setAttribute(
                        IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
                        mainClass);
                // wc.setAttribute(
                // IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
                // args);
                // saves the new config  path=\.metadata\.plugins\org.eclipse.debug.core\.launches\name.launch
                config = wc.doSave();
            }
            config.launch(ILaunchManager.DEBUG_MODE, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
参考:

http://www.blogjava.net/adolf-zhang/archive/2007/11/26/dengues_04.html

http://www.blogjava.net/jame-liu/articles/15805.html

http://www.eclipse.org/articles/Article-Java-launch/launching-java.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值