Eclipse RCP: add external directories to classpath - eclipse

I have an eclipse RCP application, where I need to add some external jar files.
The problem is that I can't add the jars simply to a plugin and add this plugin to my RCP application.
For several reasons I must only add paths to directories where the jar files are located. These jar files have to be added to the program's classpath at startup.
And the paths to the directories are a variable (e.g. they are placed in a file).
Is there a possibility to add external paths somehow to the classpath?

add external directory to classpath,there are three method:
> **1. System.setProperty("java.class.path",
> System.getProperty("java.class.path")+";"+"directory");**
File file = new File("/home/../my.jar");
URLClassLoader classloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
add.setAccessible(true);
add.invoke(classloader, new Object[] { file.toURI().toURL() });
configurate classpath variable in .bashrc

Related

Add xml file to Eclipse Plugin

I have a file model.xml i want to add to my eclipse plugin to read at runtime.
I tried adding it to the manifest.mf binary build and source build and adding it to the build.properties bin.includes but when I look in the bin folder there are only .class files.
Surely plugins can contain resources and not just .class files right?
You just list the file in the bin.includes in the build.properties - you should be able to do that in plugin.xml/MANIFEST.MF/build.properties editor.
For example in one of my plugins the build.properties is:
output.. = bin/
bin.includes = META-INF/,\
.,\
OSGI-INF/,\
plugin.xml,\
fragment.e4xmi
source.. = src/
Which includes a plugin.xml and a fragment.e4xmi file.
Note: These are not copied to the bin directory in your project. When you test your plugin they will be accessed directly in the project. When you build/export your plug-in they will be included in the plugin jar.
You use the FileLocator API to access these files - that understands where to look.
For example, to access the fragment.e4xmi shown above:
Bundle bundle = FrameworkUtil.getBundle(getClass());
// or some other way to get the current bundle
URL url = FileLocator.find(bundle, new Path("fragment.e4xmi"));

Launch External Tools from Eclipse Plug-in View

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);

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.

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);

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.