[Dengues]在Eclipse中執(zhí)行一個(gè)命令行命令,如 執(zhí)行一個(gè)bat文件,或java , jar 等. 兩種方式:一是用Runtime.exec(),二是用Eclipse的launch().
Posted on 2007-11-26 15:49 zDevil(Dengues Studio) 閱讀(4209) 評(píng)論(2) 編輯 收藏代碼可以參考:org.dengues.designer.core.launch.JavaETLProcessor.
直接進(jìn)入正題吧!第一種方法:使用Runtime.exec()方式.其實(shí)調(diào)用不難,關(guān)鍵就是要組織好它的命令參數(shù).組織參數(shù)要注意:
現(xiàn)在看Dengues源代碼里面的例子:
這個(gè)是一個(gè)執(zhí)行Java的命令行參數(shù)配置, 這里第一個(gè)直接用的java,其實(shí)更好一點(diǎn)話,你可以配置為java.exe全路徑.第二個(gè),三個(gè)參數(shù)配置JVM的內(nèi)存配置."-cp" 后跟的是要用的Jar包和Class目錄,也就是說(shuō)他是這個(gè)java運(yùn)行的classpath.最后的參數(shù)就是classname.也就是你要執(zhí)行的Class.這個(gè)如果在Dengues里面的Preference頁(yè)測(cè)試一個(gè)連接生成一個(gè)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.這樣這個(gè)ETLProcess就運(yùn)行起來(lái)了.
第二種方法:就是利用Eclipse Debug插件里面的在Dengues里面的代碼:
不過(guò)這種方式需要注意的幾個(gè)問(wèn)題:第一運(yùn)行的class需要在一個(gè)IJavaProject里面.一般可以通過(guò)JavaCore.create(IProject project).來(lái)構(gòu)造.它的優(yōu)點(diǎn)就是你可以在整個(gè)Project里面設(shè)置classpath,還可以引用其他Project的內(nèi)容;在Dengues里面JavaETLProcessor.initJavaPrject():
這樣就不需要設(shè)置執(zhí)行參數(shù).并且還可以在Console視圖里面看到執(zhí)行的結(jié)果.就是說(shuō)你在Eclipse執(zhí)行一個(gè)程序的效果.
Dengues論壇(http://groups.google.com/group/dengues/),一個(gè)很好的Eclipse開(kāi)發(fā)者樂(lè)園.
直接進(jìn)入正題吧!第一種方法:使用Runtime.exec()方式.其實(shí)調(diào)用不難,關(guān)鍵就是要組織好它的命令參數(shù).組織參數(shù)要注意:
現(xiàn)在看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 }
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 }
這個(gè)是一個(gè)執(zhí)行Java的命令行參數(shù)配置, 這里第一個(gè)直接用的java,其實(shí)更好一點(diǎn)話,你可以配置為java.exe全路徑.第二個(gè),三個(gè)參數(shù)配置JVM的內(nèi)存配置."-cp" 后跟的是要用的Jar包和Class目錄,也就是說(shuō)他是這個(gè)java運(yùn)行的classpath.最后的參數(shù)就是classname.也就是你要執(zhí)行的Class.這個(gè)如果在Dengues里面的Preference頁(yè)測(cè)試一個(gè)連接生成一個(gè)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.這樣這個(gè)ETLProcess就運(yùn)行起來(lái)了.
第二種方法:就是利用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 }
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 }
不過(guò)這種方式需要注意的幾個(gè)問(wèn)題:第一運(yùn)行的class需要在一個(gè)IJavaProject里面.一般可以通過(guò)JavaCore.create(IProject project).來(lái)構(gòu)造.它的優(yōu)點(diǎn)就是你可以在整個(gè)Project里面設(shè)置classpath,還可以引用其他Project的內(nèi)容;在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 }
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 }
這樣就不需要設(shè)置執(zhí)行參數(shù).并且還可以在Console視圖里面看到執(zhí)行的結(jié)果.就是說(shuō)你在Eclipse執(zhí)行一個(gè)程序的效果.
Dengues論壇(http://groups.google.com/group/dengues/),一個(gè)很好的Eclipse開(kāi)發(fā)者樂(lè)園.