Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

EclipseJDT Plug-in Developer Guide
Previous Page Home Next Page

Running a Java program

The JDT Debug component includes facilities for launching a Java program using the VM install that is currently configured by the user for a Java project.  

Launching a compiled Java program

Java programs that have been compiled in a Java project can be run by getting the appropriate IVMRunner for the Java project and running the class by name. The following code snippet shows how the class MyClass inside myJavaProject can be launched.


   IVMInstall vmInstall = JavaRuntime.getVMInstall(myJavaProject);
   if (vmInstall == null)
      vmInstall = JavaRuntime.getDefaultVMInstall();
   if (vmInstall != null) {
      IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
      if (vmRunner != null) {
         String[] classPath = null;
         try {
            classPath = JavaRuntime.computeDefaultRuntimeClassPath(myJavaProject);
         } catch (CoreException e) { }
         if (classPath != null) {
            VMRunnerConfiguration vmConfig = 
               new VMRunnerConfiguration("MyClass", classPath);
            ILaunch launch = new Launch(null, ILaunchManager.RUN_MODE, null);
            vmRunner.run(vmConfig, launch, null);
         }
      }
   }

Another way to launch a Java program is to create a Java application launch configuration, and launch it. The following snippet shows how the class MyClass inside myJavaProject can be launched using a simple launch configuration. By default, the resulting running application uses the JRE and classpath associated with myJavaProject.


   ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
   ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
   ILaunchConfigurationWorkingCopy wc = type.newInstance(null, "SampleConfig");
   wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "myJavaProject");
   wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "myClass");
   ILaunchConfiguration config = wc.doSave();	
   config.launch(ILaunchManager.RUN_MODE, null);


 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire