Launching a compiled Java program

本文将指导您如何使用Eclipse IDE在已配置的JVM环境下运行Java程序,包括编译后的Java程序和通过Java应用启动配置运行的方式。

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

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_run.htm

 

http://camel-route-viewer.googlecode.com/svn-history/r136/trunk/src/com/googlecode/camelrouteviewer/launcher/CamelLaunchShortcut.java

 

 

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 MyClassinside 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值