eclipse插件开发--获取当前项目路径
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; /**
* Our sample action implements workbench action delegate.
* The action proxy will be created by the workbench and
* shown in the UI. When the user tries to use the action,
* this delegate will be created and execution will be
* delegated to it.
* @see IWorkbenchWindowActionDelegate
*/
@SuppressWarnings({ "restriction" })
public class RuntimePower implements IWorkbenchWindowActionDelegate {
// private IWorkbenchWindow window;
/**
* The constructor.
*/
public RuntimePower() {
} /**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
try {
String path = getProjectPath();
IProject myProject = getCurrentProject();
path = path + myProject.getFullPath();
runProc("/chai/run_java.sh");
String command = path + "/Debug" + myProject.getFullPath();
runProc("/chai/run.sh" + " " + command); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static String runProc(String para) throws IOException
{
Process p = Runtime.getRuntime().exec(para);
String pid = getPID(p);
return pid;
}
public static String getPID(Process p) throws IOException
{
BufferedReader br = new
BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
line = br.readLine();
return line;
}
public static String getProjectPath()
{
String path = null;
path = Platform.getLocation().toString();
return path;
} public static IProject getCurrentProject(){
ISelectionService selectionService =
Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService(); ISelection selection = selectionService.getSelection(); IProject project = null;
if(selection instanceof IStructuredSelection) {
Object element = ((IStructuredSelection)selection).getFirstElement(); if (element instanceof IResource) {
project= ((IResource)element).getProject();
}else if (element instanceof PackageFragmentRootContainer)
{
IJavaProject jProject =
((PackageFragmentRootContainer)element).getJavaProject();
project = jProject.getProject();
}else if (element instanceof IJavaElement) {
IJavaProject jProject= ((IJavaElement)element).getJavaProject();
project = jProject.getProject();
}else if (element instanceof ICElement){
ICProject cProject = ((ICElement)element).getCProject();
project = cProject.getProject();
} }
return project;
}
/**
* Selection in the workbench has been changed. We
* can change the state of the 'real' action here
* if we want, but this can only happen after
* the delegate has been created.
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
} /**
* We can use this method to dispose of any system
* resources we previously allocated.
* @see IWorkbenchWindowActionDelegate#dispose
*/
public void dispose() {
} /**
* We will cache window object in order to
* be able to provide parent shell for the message dialog.
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
// this.window = window;
}
}
其中,run_java.sh如下:
#!/bin/bash
java -jar /chai/Monitor.jar &
echo $!
Monitor.jar 为用jfreechart写的能耗时序图代码,打包而成
run.sh如下:
#!/bin/bash
$ &
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();
eclipse插件开发--获取当前项目路径的更多相关文章
- asp.net 获取当前项目路径
方法一://获取当前项目的路径System.AppDomain.CurrentDomain.BaseDirectory.ToString(); // 得到的是当前项目的根目录取的值:F://Pro ...
- Java获取web项目路径
File f = new File(WebPath.class.getResource("/").getPath()); String path = f.getParentFile ...
- eclipse中的web项目路径和发布好的项目路径
现在企业开发中,我们都会创建一个javaWeb工程,在eclipse中指的是新建一个dynamic web project,创建完工程之后,我们在IDE中大体看到如下的工程目录: 我们主要关心的文件夹 ...
- java获取当前项目路径System.getProperty("user.dir")
System.getProperty("user.dir") 就是项目的文件夹绝对路径
- springMVC 获取本地项目路径 及后整理上传文件的方法
String path=request.getSession().getServletContext().getRealPath("upload/img/product"); // ...
- javascript 获取当前部署项目路径
javascript 获取当前部署项目路径 ========================================= javascript获取当前部署项目路径: 主要用到Location 对 ...
- 将Maven项目转换成Eclipse支持的Java项目
当我们通过模版(比如最简单的maven-archetype-quikstart插件)生成了一个maven的项目结构时,如何将它转换成eclipse支持的java project呢? 1. 定位到mav ...
- 获取web项目的绝对路径的方法总结
一.用Jsp获取 1.获取文件的绝对路径 String file="文件";(例如:data.mdb) String path=application.getRealPath(fi ...
- java web项目获取项目路径
注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...
随机推荐
- ZJOI 游记
在备战YZ提前招生考时去ZJOI玩了趟,ZJ果然人才辈出= =神犇讲课各种神听不懂啊orz day 0 Mon. 上午在AB班愉快地玩耍,下午就去HZ了. HZ真热啊... 学军也是节约= =空调都不 ...
- Linux下常用程序的代理服务器(proxy)配置
Linux下有很多程序都只有命令行接口,对于这类程序,它们通过代理服务器(proxy)访问网络的方式也不尽相同.在本文中Easwy总结了一些常用Linux程序配置代理服务器的方法. [ 通用代理服务器 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------ViewBag、ViewData和TempData的使用和区别
本文转自大卫Baby ViewBag和ViewData其实是互通的ViewBag和ViewData的区别:ViewBag 不再是字典的键值对结构,而是 dynamic 动态类型,它会在程序运行的时候动 ...
- 3Sum & 4Sum
3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...
- 【leetcode】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【转】idea 用maven骨架生成项目速度慢的问题
转自:http://9leg.com/maven/2015/02/01/why-is-mvn-archetype-generate-so-low.html 最近从IntelliJ Idea 14的Co ...
- iOS 和 Android 中的Alert
iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...
- Windows远程桌面连接如何直接使用剪贴板功能
连接到Windows Server服务器时,通常使用远程桌面连接,那么如果有些时候按照习惯复制本地文件到远程服务器发现无法粘贴怎么办,这个时候稍微设置一下就OK了, 首先重新运行远程桌面连接,在登陆界 ...
- mybatis随机生成可控制主键的方式
mybatis生成的主键,一般都是用数据库的序列,可是还有不同的写法,比如: 一.NUMBER类型的主键 <insert id="insertPeriodical" para ...
- HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...