How to use common paths in Eclipse Application and the generated JAR? - eclipse

I am developing an Eclipse plugin, and most of the time I run as "Eclipse Application", but sometimes I also export as a JAR.
I have two source folders, "src/" and "icons/". The problem is that when I run as "Eclipse Application" the path to the Icons are "icons/com/...", while when I generate the JAR the paths become "/com/...".
I would like a common path that will work whether I generate a JAR or run as an Eclipse Application. How can I achieve this?

Don't specify icons as a source folder just leave it as a plain folder. Do include it in the build.properties so that it is included in the built plug-in.
Use the FileLocator class to locate objects in plug-ins.
For images you can use something like:
String path = "icons/xxxxxx.jpg"; // TODO your path to the image
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
Note that the URL from FileLocator.find is only suitable for use by Eclipse APIs. You can convert it to a more normal URL using:
URL fileURL = FileLocator.toFileURL(url);

Related

Relative path doesn't work when exporting to runabble jar on Eclipse

I'm doing a Java project in Eclipse. I am using some relative paths such as:
File htmlTemplateFile = new File("src/templateGeno2Pheno.html");
instead of absolute paths:
File htmlTemplateFile = new File("/Users/.../Documents/workspace/SeqAnalysis/src/templateGeno2Pheno.html");
Everything works very well when I run it with Eclipse, but once I export it to a runnable JAR and execute it, it doesn't work.
This is my folder structure:
Here is my code:
After packaging into a JAR then everything, including your HTML templates, are zipped up into that JAR and therefore filesystem paths aren't going to work for you. Java doesn't have a file addressing system like your browser/html.
One option is to load those html templates using ClassLoader.getResourceAsStream API.
Change your code to:
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/templateGeno2Pheno.html");
String htmlString = IOUtils.toString(stream);
IOUtils is from the Apache Commons IO library.
HTH

How to access the member files and folders of a plug-in in Eclipse

We have a plug-in that is exported to an RCP product. In the plug-in, there is a folder that has some files. How can I access the plug-in files under a certain folder in Eclipse programatically?
Use the org.eclipse.core.runtime.FileLocator class to access files in a plugin.
Bundle bundle = ... bundle containing the files
IPath path = new Path("relative path in plug-in of the file");
URL url = FileLocator.find(bundle, path, null);
URL fileUrl = FileLocator.toFileURL(url);
The url returned by the FileLocator.find method uses an Eclipse specific scheme and can only be used with certain Eclipse APIs.
The FileLocator.toFileURL call converts the URL to a normal file URL, it may be necessary to unpack the plug-in jar to a temporary location in order to do this.
You can get the Bundle using
Bundle bundle = FrameworkUtil.getBundle(getClass());
which gets the bundle containing the current class or
Bundle bundle = Platform.getBundle("plugin id");
to access a bundle by plug-in id.

MyBatis looking for xml files in Tomcat directory

I'm testing a web application with Eclipse + Tomcat, Eclipse deploys the web application files and launches Tomcat, and the application runs fine. But when MyBatis is trying to open it's XML configuration files, it looks for them in
C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\persistence\db\oracle.xml
instead of the correct place:
C:\workspace\mywebapp\src\persistence\db\oracle.xml
Where is MyBatis supposed to look for XML files?
EDIT:
This is where I specify the relative path:
String cfgFile = "persistence/db/oracle.xml";
Reader reader = Resources.getResourceAsReader(cfgFile);
session.put(db, new SqlSessionFactoryBuilder().build(reader));
Resources.getResourceAsReader looks files in classpath. For web application running in tomcat classpath consist of WEB-INF/classes and all jars from WEB-INF/lib and tomcat folders like $TOMCAT_HOME\lib.
The issue you encounter most probably is caused by the fact that oracle.xml file is not added to deployment. It looks like c:\workspace\myweapp\src is not among source folder of eclipse project so eclipse doesn't copy files from it to the folder which is deployed to tomcat. Depending on your existing project structure you may need to create subfolder in src and add persistence with all subfolders there. This will allow you to avoid clash if some subfolder of src is already a source folder in eclipse. I would recommend to use maven project structure:
src
main
* java
you java source code here organized by package
* resources
persistence
I marked folders which should be added as source folder to eclipse with *.
Please note that it is not correct to say that C:\workspace\mywebapp\src\persistence\db\oracle.xml is a correct place to search for it. After you create a war to deploy it on production this path most probably will not be available at your production server. What you really need is to include persistence\db\oracle.xml to the war in appropriate place (under WEB-INF/classes).
Maybe you need another class loader 1. Try this:
String cfgFile = "persistence/db/oracle.xml";
ClassLoader classloader = Thread.currentThread().getContextClassLoader()
Reader reader = Resources.getResourceAsReader(classloader, cfgFile);
Notes
See Difference between thread's context class loader and normal classloader and you may want to see the code of org.apache.ibatis.io.Resources. You find it here.

Including a file in the Eclipse RCP bundle working directory

I want to include a file (text, image, etc) in the root directory of an Eclipse plugin.
When I run the program using a test main method, I can find the file in the working directory.
But when I run the plugin as an Eclipse application, the working directory contains different files depending on the operating system and I can't find the text file.
I tried adding the file to the binary build in the build tab of the xml (build.properties). It doesn't work still.
How will I find the path to the text file? How can I make sure that the text file is exported with the plugin?
Thanks
When you build your Eclipse plugin everything in the plugin is put in to a jar file in the 'plugins' directory. As long as your file is listed in the 'build.properties' (the build tab) it will be included in the jar.
To access a file in the jar use:
Bundle bundle = Platform.getBundle("your plugin id");
URL url = FileLocator.find(bundle, new Path("path in plugin"), null);
The URL returned is suitable for passing to various Eclipse APIs but cannot be used with normal Java APIs such as File. To convert it to a file URL use:
URL fileURL = FileLocator.toFileURL(url);
This will copy the file out of the jar in to a temporary location where it can be accessed as a normal file.
You can also get the Bundle using:
Bundle bundle = FrameworkUtil.getBundle(getClass());
which avoids having to include the plug-in id.

How to get folder from OSGI bundle?

I have own OSGI bundle that have folder inside with some resources. How to read that folder from bundle and get all children of it?
I used before following code.:
Bundle bundle = Platform.getBundle(bundleID);
URL fileURL = bundle.getEntry(templatePath);
URL url = FileLocator.resolve(fileURL);
This code workd utils I exported Eclipse application as Eclipse product.
But I have URL is not hierachical excepiton.
You can use the findEntries method on Bundle to locate files in the bundle and any attached fragments:
Bundle bundle = Platform.getBundle(bundleID);
Enumeration<URL> urls = bundle.findEntries("/folder", "*", false);
You should write your code in terms of InputStreams and use the method suggested by #NickWilson to retrieve the input streams and use them in your program.
But if you really need that folder exploded on the disk somewhere, you would use org.eclipse.core.runtime.FileLocator.toFileURL(URL) instead of resolve(URL).