Launch External Tools from Eclipse Plug-in View - eclipse

I'm building a simple Eclipse plug-in out of preexisting Java application project that relays on 2 external files, one x-executable/application and one .sh script.
The call is implemented in application like this, (which wouldn't work in plug-in):
Process p = new ProcessBuilder("external/application_name", "-d", path).start();
I used External Tool Configuration to define how I want this external files to be launch (when user clicks button on View) and I've exported configuration (example of one):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/softwareevolution/external/application_name}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="-d ${project_loc}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>
How can I have this application install along with Eclipse plug in,
or a as a part of it? (see #howlger answer billow) - I set plugin to install as directory /
Connected plugin to Feature project- checked unpack after
installation - and exported Feature project. Select application's
folder on Build/Binary build.
Can I then make use out of this exported .launch files, and if so
under which extension point should I include them in plugin.xml? -
No. (see #greg-449)
The application is supposed to produce 2 files on the path where it
is executed from. I am facing permission denied when trying to
launch it in terminal from plug-in's install directory but not when
launching in workspace. (see #howlger answer billow) - Upon exporting the plugin, initial
permissions of application had changed. Used instructions in p2.inf
to chmod them back.
The newly generated files (from runing .sh script) are missing write permission.
ProcessBuilder
After finally setting up plugin correctly and adding ProcessBuilder I was getting exception message : Cannot run program "rfind_20" (in
directory
"home/adminuser/.p2/pool/plugins/rFindTest3_1.0.0.201809030453/external"
error=2:, No such file or directory
File rfind_20 did exist and permission were 777. The targeted project also existed.
Although the working directory was set to applications folder, the application name was not enough, the absolute path was required as
command argument.
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IProject project= sampleGetSelectedProject();
ProcessBuilder pb;
Process rfind, ajust, copy;
Bundle bundle = FrameworkUtil.getBundle(getClass());//Bundle bundle = Platform.getBundle("rFindTest3");
URL url = FileLocator.find(bundle, new Path("external/rfind_20"), null);
URL dirurl = FileLocator.find(bundle, new Path("external/"), null);
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
try {
MessageDialog.openInformation(
window.getShell(),
"Test",
project.getProject().getLocation().toString());
url = FileLocator.toFileURL(url);
dirurl = FileLocator.toFileURL(dirurl);
pb = new ProcessBuilder(url.getPath(), "-d", project.getProject().getLocation().toString());
//no matter the working directory the absolute path was required!! sending "rfind_20" as command did not work as command
pb.directory(new File(dirurl.getFile()));
rfind = pb.start();
rfind.waitFor();
rfind.destroy();
}catch(Exception e) {
MessageDialog.openInformation(
window.getShell(),
"Test",
e.getMessage());
}
return null;
}
The only remaining mystery is why my sampleGetProject() method wouldn't work in Plug-in Perspective. So just keep in mind to switch to other Perspectives when testing your plug-in.

There are two ways to ship an application as part of a plugin and run it via ProcessBuilder (the *.launch file cannot be used inside a plugin for that):
Extract the executable files from the plugin JAR to a (temp) directory and change their file permissions before running them
Install the plugin as a directory:
In META-INF/MANIFEST.MF add the line Eclipse-BundleShape: dir (see "The Eclipse-BundleShape Header" in Eclipse help - Platform Plug-in Developer Guide - OSGi Bundle Manifest Headers)
Create a Feature Project and connect your plug-in in Included Plug-in, check "Unpack the plug-in archive after the installation"
Create a META-INF/p2.inf file that contains the following (see Eclipse help - Platform Plug-in Developer Guide: "Touchpoint Instruction Advice" in Customizing p2 metadata and "chmod" in Provisioning Actions and Touchpoints):
instructions.install = \
chmod(targetDir:${artifact.location},targetFile:path/to/executable1,permissions:755);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/executable2,permissions:733);\
chmod(targetDir:${artifact.location},targetFile:path/to-executale_which_generates_files/,permissions:766);
instructions.install.import = org.eclipse.equinox.p2.touchpoint.eclipse.chmod

If you have a xxx.launch file in the workspace you can launch it using
IFile file = ... get IFile for workspace file
ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file);
DebugUITools.launch(config, ILaunchManager.RUN_MODE, false);
If you have an executable as part of a plug-in then you can't use a .launch file. Instead use FileLocator to get the location of the executable and run it with ProcessBuilder
Bundle bundle = FrameworkUtil.getBundle(getClass());
URL url = FileLocator.find(bundle, new Path("relative path to executable"));
url = FileLocator.toFileURL(url);

Related

OpenCV Exporting Runnable JAR in Eclipse

While Exporting Runnable JAR in Eclipse, Got error as no open cv in java.library.path.
Included Steps :-
Created User Library (ex OpenCV320) in eclipse and added in project build path also dll (as my system is 64 bit "C:\OpenCV\opencv\build\java\x64") opencv_java320.dll is set as Native library Location.
While exporting runnable jar selected "Extract required libraries into generated jar".
Here is the solution
No need to create separate user library.
Add below code.
String libraryPath = "C:\OpenCV\opencv\build\java\x64";
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
3.And export the runnable jar.

How to get resources path from an Eclipse plugin

I am developing an Eclipse plugin using Eclipse Plugin-in-project where it will add a menu item in the toolbar.
My plugin project is depending on one file which is located in the same plugin project I want the path of that file.
Below is the sample code I have used to get the path:
Bundle bundle = Platform.getBundle("com.feat.eclipse.plugin");
URL fileURL = bundle.getEntry("webspy/lib/file.txt");
File file = null;
String path=null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
path = file.getAbsolutePath();
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
While running from Eclipse Run as > Eclipse Application it is giving me the correct path. But when I export my plugin as jar and add it in my Eclipse plugins folder its not giving me the correct path.
Please help me to resolve this!
Use:
URL url = FileLocator.find(bundle, new Path("webspy/lib/file.txt"), null);
url = FileLocator.toFileURL(url);
File file = URIUtil.toFile(URIUtil.toURI(url));
When your files are packed in a jar FileLocator.toFileURL will copy them to a temporary location so that you can access them using File.
Files in jars have no java.io.File as they are in the JAR file.
You probably want FileLocator.openStream(...) to read the file contents.
If you really want a java.io.File, then you can consider using Eclipse EFS and its IFileStore.toLocalFile(...) to extract the file for you to a temporary directory. Something like EFS.getStore(fileURL).toLocalFile(EFS.CACHE, null) should do what you want, but remember that will put the file in the temp directory and delete it on Eclipse exit.

Use Eclipse Runtime Configuration to start jar file

I need to start a Java program using an Eclipse Runtime Configuration (ILaunchConfiguration). However, I want to provide the program to run as a .jar file (as part of the plugin), not as an Eclipse project.
It seems in order to start a Java program from an Eclipse Runtime Configuration I need to specify a project (and main class).
How can I use the Configuration framework to start an arbitrary .jar file?
This article helps:
http://eclipse.org/articles/Article-Java-launch/launching-java.html
I use the following code to run a .jar file which is inside my plugin's lib directory:
IPath path = new Path("lib" + File.separator + "some.jar");
Bundle bundle = Platform.getBundle(IDs.PLUGIN_ID);
URL url = FileLocator.find(bundle, path, null);
URI uri = FileLocator.resolve(url).toURI();
File file = URIUtil.toFile(uri);
IPath resolvedPath = new Path(file.toString());
IRuntimeClasspathEntry jar = JavaRuntime.newArchiveRuntimeClasspathEntry(resolvedPath);
IPath systemLibsPath = new Path(JavaRuntime.JRE_CONTAINER);
IRuntimeClasspathEntry systemLibsEntry =
JavaRuntime.newRuntimeContainerClasspathEntry(systemLibsPath, IRuntimeClasspathEntry.STANDARD_CLASSES);
List<String> classpath = new LinkedList<>();
classpath.add(aproveJar.getMemento());
classpath.add(systemLibsEntry.getMemento());
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath);
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, false);

Eclipse how to reference file in a different src under the same project

My current setup in eclipse is like this:
trunk
--working/src
--resources
I have a java file inside a package under working/src and I am trying to retrieve a file in the resources. The path I am using is "../resources/file.txt". However I am getting an error saying that the file does not exist.
Any help would be appreciated thanks!
Considering your structure
I have package like
trunk
working/src/FileRead.java
resources/name list.txt
Following Code might solve your problem
package working.src;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class FileRead {
public FileRead() {
URL url = getClass().getResource("/resources/name list.txt");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String nameList;
while ((nameList = in.readLine()) != null) {
System.out.println(nameList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileRead();
}
}
Files will be referenced relative to your project path, so use "resources/file.txt" to reference the file.
However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file.txt" must exist relative to your JAR.
It depends on how you have specified your Java Build Path inside eclipse. I have tested two setups with different results:
Define the directory working/src only as build path. You can get the information what is in your build path through: Select project > Properties > Java Build Path > Tab Source. There are all source folders listed. You see there that there is a default output folder defined. All resources that are contained in a build path are copied to the default output folder, and are then available for the eclipse classes there. You can check that in the resource perspective in your bin folder if the resource is copied there. In this setup, only the classes are generated, resources are not copied (and therefore not found).
Define the directory working/src and resources as build path. Then the resource is copied and may then found by the path "file.txt"
So eclipse has a simple build process included and hides that from the developer, but it is a build process nonetheless.

Programmatically adding a library to an Eclipse project

How can I create a new build path entry for any *.jar file and add this classpath entry to the build path of an Eclipse project.
I have a plugin that should automatically setup my target project. So this project needs to have some library imports and I want to add this imports automatically using a wizard. The user just selects the location of a certain SDK and then some libraries have to be linked with the target project.
However, I found some references:
Importing libraries in Eclipse programmatically
How to add a folder to java build path as library, having multiple jars or entries in it?
Unfortunately, I failed to implement the second solution as I cannot find the classes IClasspathContainer, JavaCore and IJavaProject.
I'm using Eclipse Helios and JDK. Do I need any additional libraries to make changes to the build path or is there a simpler solution to import a jar library programmatically?
Regards,
Florian
I'm assuming that you are creating a plugin and need your plugin to manage the extra jars added to the classpath.
As you mention, you need to create a custom classpath container. First, create the classpath container extension by exending this extension point:
org.eclipse.jdt.core.classpathContainerInitializer
Then, you create a class that implements org.eclipse.jdt.core.IClasspathContainer and associate it with the extension point you just created.
You mention that you cannot find the org.eclipse.jdt.core.IClasspathContainer interface. You need to make sure that your plugin references the org.eclipse.jdt.core plugin in its MANIFEST.MF.
Here you can find some examples, how to define new classpath entries and classpath containers to java projects. I think it would handy for someone reading this question.
In order to get access to IJavaProject etc, goto your plugin.xml and add org.eclipse.jdt.core to the classpath. Thereafter you can import those packages into your project.
String projectName = "MyProject"; // project to add a library to
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject jProject = JavaCore.create(project);
for(File file : new File("path-to-some-directory-of-libraries-to-add").listFiles()){
if(file.isFile() && file.getName().endsWith(".jar")){
addProjectLibrary(jProject, file);
}
}
private static void addProjectLibrary(IJavaProject jProject, File jarLibrary) throws IOException, URISyntaxException, MalformedURLException, CoreException {
// copy the jar file into the project
InputStream jarLibraryInputStream = new BufferedInputStream(new FileInputStream(jarLibrary));
IFile libFile = jProject.getProject().getFile(jarLibrary.getName());
libFile.create(jarLibraryInputStream, false, null);
// create a classpath entry for the library
IClasspathEntry relativeLibraryEntry = new org.eclipse.jdt.internal.core.ClasspathEntry(
IPackageFragmentRoot.K_BINARY,
IClasspathEntry.CPE_LIBRARY, libFile.getLocation(),
ClasspathEntry.INCLUDE_ALL, // inclusion patterns
ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
null, null, null, // specific output folder
false, // exported
ClasspathEntry.NO_ACCESS_RULES, false, // no access rules to combine
ClasspathEntry.NO_EXTRA_ATTRIBUTES);
// add the new classpath entry to the project's existing entries
IClasspathEntry[] oldEntries = jProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
newEntries[oldEntries.length] = relativeLibraryEntry;
jProject.setRawClasspath(newEntries, null);
}
Note that as Andrew Eisenberg mentioned, you need to include the org.eclipse.jdt.core plugin dependency in your plugin's MANIFEST.MF.
Note that you may also need to programmatically refresh the project too.