代码可以参考:org.dengues.designer.core.launch.JavaETLProcessor.
直接进入正题吧!第一种方法:使用Runtime.exec()方式.其实调用不难,关键就是要组织好它的命令参数.组织参数要注意:
现在看Dengues源代码里面的例子:
1 public String[] getCommandLine(String[] libpath) {
2 String command = "java"; //$NON-NLS-1$
3 StringBuffer libPath = new StringBuffer();
4 String separator = System.getProperty("path.separator");
5 for (String string : libpath) {
6 libPath.append(FileUtils.getOSPath(string) + separator);
7 }
8 // init project_path
9 String projectPath;
10 IFolder classesFolder = getJavaProject().getProject().getFolder(JavaProcessorUtil.JAVA_PROJ_CLASSES); //$NON-NLS-1$
11 IPath projectFolderPath = classesFolder.getFullPath().removeFirstSegments(1);
12 projectPath = Path.fromOSString(project.getLocation().toOSString()).append(projectFolderPath).toOSString();
13
14 // init class name
15 IPath classPath = getCompiledCodePath().removeFirstSegments(1);
16 String className = classPath.toString().replace('/', '.');
17
18 return new String[] { new Path(command).toPortableString(), "-Xms256M", "-Xmx1024M", "-cp", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
19 libPath.toString() + new Path(projectPath).toPortableString(), className };
20 }
这个是一个执行Java的命令行参数配置, 这里第一个直接用的java,其实更好一点话,你可以配置为java.exe全路径.第二个,三个参数配置JVM的内存配置."-cp" 后跟的是要用的Jar包和Class目录,也就是说他是这个java运行的classpath.最后的参数就是classname.也就是你要执行的Class.这个如果在Dengues里面的Preference页测试一个连接生成一个Command命令: Command line: java -Xms256M -Xmx1024M -cp E:\TEMP\db driver\mysql-connector-java-5.1.0-bin.jar;E:/workspaces/runtime-dengues.product/.Java/classes dengues.testcomponents.shadow.shadow_process.这样这个ETLProcess就运行起来了.
第二种方法:就是利用Eclipse Debug插件里面的在Dengues里面的代码:
1 public static ILaunch launch(IJavaProject proj, String name, String mainClass, String args) throws CoreException {
2 ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
3 ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
4 ILaunchConfiguration config = null;
5 // if the configuration already exists, use it!
6 ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
7 for (int i = 0; i < configurations.length; i++) {
8 if (configurations[i].getName().equals(name))
9 config = configurations[i];
10 }
11 // else create a new one
12 if (config == null) {
13 ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name);
14 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, proj.getProject().getName());
15 // current directory should be the project root
16 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, proj.getProject().getLocation().toString());
17 // use the suplied args
18 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainClass);
19 wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, args);
20 wc.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, PROCESS_FACTORY_ID);
21 // saves the new config
22 config = wc.doSave();
23 }
24 return config.launch(ILaunchManager.RUN_MODE, null);
25 }
不过这种方式需要注意的几个问题:第一运行的class需要在一个IJavaProject里面.一般可以通过JavaCore.create(IProject project).来构造.它的优点就是你可以在整个Project里面设置classpath,还可以引用其他Project的内容;在Dengues里面JavaETLProcessor.initJavaPrject():
1 public void initJavaProject() {
2 if (javaProject != null) {
3 return;
4 }
5 try {
6 initProject();
7 javaProject = JavaCore.create(project);
8 IClasspathEntry classpathEntry = JavaCore.newSourceEntry(new Path("/" + project.getName() + "/" //$NON-NLS-1$ //$NON-NLS-2$
9 + JavaProcessorUtil.JAVA_PROJ_SRC));
10 IClasspathEntry jreClasspathEntry = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")); //$NON-NLS-1$
11 List<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>();
12 classpath.add(classpathEntry);
13 classpath.add(jreClasspathEntry);
14
15 classpath.addAll(addDriverClasses());
16 classpath.addAll(addVariable(javaProject, "DENGUES_LIB", Activator.PLUGIN_ID));
17 // add the classpath variables
18 for (EClasspathVariables var : EClasspathVariables.values()) {
19 IClasspathEntry hsqlClasspathEntry = JavaCore.newVariableEntry(new Path(var.toString()), null, null); //$NON-NLS-1$
20 classpath.add(hsqlClasspathEntry);
21 }
22 IFolder sourceFolder = project.getFolder(new Path(JavaProcessorUtil.JAVA_PROJ_SRC));
23 if (!sourceFolder.exists()) {
24 sourceFolder.create(false, true, null);
25 }
26 IFolder runtimeFolder = project.getFolder(new Path(JavaProcessorUtil.JAVA_PROJ_CLASSES));
27 if (!runtimeFolder.exists()) {
28 runtimeFolder.create(false, true, null);
29 }
30 javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[0]), null);
31 javaProject.setOutputLocation(new Path("/" + project.getName() + "/" + JavaProcessorUtil.JAVA_PROJ_CLASSES), null); //$NON-NLS-1$ //$NON-NLS-2$
32 // javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.6"); //$NON-NLS-1$ //$NON-NLS-2$
33 } catch (Exception e) {
34 log.error("initJavaProject Exception: ", e); //$NON-NLS-1$
35 }
36 }
这样就不需要设置执行参数.并且还可以在Console视图里面看到执行的结果.就是说你在Eclipse执行一个程序的效果.
Dengues论坛(http://groups.google.com/group/dengues/),一个很好的Eclipse开发者乐园.