Skip to main content
Jump to: navigation, search

FAQ How do I launch a Java program?

JDT has support for launching Java programs. First, add the following plug-ins to your dependent list:

  • org.eclipse.debug.core
  • org.eclipse.jdt.core
  • org.eclipse.jdt.launching

With those plug-ins added to your dependent plug-in list, your Java program can be launched using the JDT in two ways. In the first approach, an IVMRunner uses the currently installed VM, sets up its classpath, and asks the VM runner to run the program:

   void launch(IJavaProject proj, String main) {
      IVMInstall vm = JavaRuntime.getVMInstall(proj);
      if (vm == null) vm = JavaRuntime.getDefaultVMInstall();
      IVMRunner vmr = vm.getVMRunner(ILaunchManager.RUN_MODE);
      String[] cp = JavaRuntime.
      VMRunnerConfiguration config = 
         new VMRunnerConfiguration(main, cp);
      ILaunch launch = new Launch(null, 
         ILaunchManager.RUN_MODE, null);, launch, null);

The second approach is to create a new launch configuration, save it, and run it. The cfg parameter to this method is the name of the launch configuration to use:

   void launch(IJavaProject proj, String cfg, String main) {
      DebugPlugin plugin = DebugPlugin.getDefault();
      ILaunchManager lm = plugin.getLaunchManager();
      ILaunchConfigurationType t = lm.getLaunchConfigurationType(
      ILaunchConfigurationWorkingCopy wc = t.newInstance(
        null, cfg);
      ILaunchConfiguration config = wc.doSave();   
      config.launch(ILaunchManager.RUN_MODE, null);

More information is available at Help > Help Contents > JDT Plug-in Developer Guide > JDT Debug > Running Java code.

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top