代碼可以參考: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開發者樂園.