1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import org.eclipse.jface.action.IAction; 5 import org.eclipse.jface.viewers.ISelection; 6 import org.eclipse.jface.viewers.IStructuredSelection; 7 import org.eclipse.ui.ISelectionService; 8 import org.eclipse.ui.IWorkbenchWindow; 9 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 10 import org.eclipse.ui.internal.Workbench; 11 import org.eclipse.core.resources.IProject; 12 import org.eclipse.core.resources.IResource; 13 import org.eclipse.core.runtime.Platform; 14 import org.eclipse.jdt.core.IJavaElement; 15 import org.eclipse.jdt.core.IJavaProject; 16 import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; 17 import org.eclipse.cdt.core.model.ICElement; 18 import org.eclipse.cdt.core.model.ICProject; 19 20 /** 21 * Our sample action implements workbench action delegate. 22 * The action proxy will be created by the workbench and 23 * shown in the UI. When the user tries to use the action, 24 * this delegate will be created and execution will be 25 * delegated to it. 26 * @see IWorkbenchWindowActionDelegate 27 */ 28 @SuppressWarnings({ "restriction" }) 29 public class RuntimePower implements IWorkbenchWindowActionDelegate { 30 // private IWorkbenchWindow window; 31 /** 32 * The constructor. 33 */ 34 public RuntimePower() { 35 } 36 37 /** 38 * The action has been activated. The argument of the 39 * method represents the 'real' action sitting 40 * in the workbench UI. 41 * @see IWorkbenchWindowActionDelegate#run 42 */ 43 public void run(IAction action) { 44 try { 45 String path = getProjectPath(); 46 IProject myProject = getCurrentProject(); 47 path = path + myProject.getFullPath(); 48 runProc("/chai/run_java.sh"); 49 String command = path + "/Debug" + myProject.getFullPath(); 50 runProc("/chai/run.sh" + " " + command); 51 52 } catch (IOException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 } catch (SecurityException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } catch (IllegalArgumentException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 } 63 64 public static String runProc(String para) throws IOException 65 { 66 Process p = Runtime.getRuntime().exec(para); 67 String pid = getPID(p); 68 return pid; 69 } 70 public static String getPID(Process p) throws IOException 71 { 72 BufferedReader br = new 73 BufferedReader(new InputStreamReader(p.getInputStream())); 74 String line = null; 75 line = br.readLine(); 76 return line; 77 } 78 public static String getProjectPath() 79 { 80 String path = null; 81 path = Platform.getLocation().toString(); 82 return path; 83 } 84 85 public static IProject getCurrentProject(){ 86 ISelectionService selectionService = 87 Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService(); 88 89 ISelection selection = selectionService.getSelection(); 90 91 IProject project = null; 92 if(selection instanceof IStructuredSelection) { 93 Object element = ((IStructuredSelection)selection).getFirstElement(); 94 95 if (element instanceof IResource) { 96 project= ((IResource)element).getProject(); 97 }else if (element instanceof PackageFragmentRootContainer) 98 { 99 IJavaProject jProject = 100 ((PackageFragmentRootContainer)element).getJavaProject(); 101 project = jProject.getProject(); 102 }else if (element instanceof IJavaElement) { 103 IJavaProject jProject= ((IJavaElement)element).getJavaProject(); 104 project = jProject.getProject(); 105 }else if (element instanceof ICElement){ 106 ICProject cProject = ((ICElement)element).getCProject(); 107 project = cProject.getProject(); 108 } 109 110 } 111 return project; 112 } 113 /** 114 * Selection in the workbench has been changed. We 115 * can change the state of the 'real' action here 116 * if we want, but this can only happen after 117 * the delegate has been created. 118 * @see IWorkbenchWindowActionDelegate#selectionChanged 119 */ 120 public void selectionChanged(IAction action, ISelection selection) { 121 } 122 123 /** 124 * We can use this method to dispose of any system 125 * resources we previously allocated. 126 * @see IWorkbenchWindowActionDelegate#dispose 127 */ 128 public void dispose() { 129 } 130 131 /** 132 * We will cache window object in order to 133 * be able to provide parent shell for the message dialog. 134 * @see IWorkbenchWindowActionDelegate#init 135 */ 136 public void init(IWorkbenchWindow window) { 137 // this.window = window; 138 } 139 }
其中,run_java.sh如下:
1 #!/bin/bash 2 java -jar /chai/Monitor.jar & 3 echo $!
Monitor.jar 为用jfreechart写的能耗时序图代码,打包而成
run.sh如下:
1 #!/bin/bash 2 $1 & 3 echo $!
$1表示传入给run.sh的第一个参数,如 run.sh "echo helloworld"
查看interface 可以查找:http://help.eclipse.org
以下内容转自:http://blog.csdn.net/u010436741/article/details/9132925
对于CDT的安装:
解决方法:
1. 下载地址:http://www.eclipse.org/cdt/downloads.php
我下载的是最新版本cdt-master-8.1.2.zip
2. 安装到eclipse上
步骤:Eclipse ->Help -> Install New Software -> 点击ADD添加 -> 名称填写CDT ,地址那里选择Archive,然后选择已下好的cdt-master-8.1.2.zip -> OK下一步下一步…最后重启Eclipse
当要在其他按钮里触发获取项目,或者获取Package Explorer里当前选项时,可以如下:
IStructuredSelection structured =
(IStructuredSelection)PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
//或者 "org.eclipse.ui.navigator.ProjectExplorer"
Object selected = structured.getFirstElement();
if(selected instanceof IResource){
IProject project = ((IResource)selected).getFirstElement();