Unable to get Image file URL from eclipse plugin project Bundle - java

I am trying to get image from a bundle in eclipse plugin project. But it always return null. Please find the project structure below. I want to get the URL of an image "icon_4.png" which is under "images/EmailTemplatesOutput/assets/icon_4.png". I have tried different ways. But it returns null.
Project Structure is :
String path = "icon_4.png";
Bundle bundle = Platform.getBundle("BulkDemo");
URL url = FileLocator.find(bundle, new org.eclipse.core.runtime.Path(path), null);
ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
Image image = imageDesc.createImage();

Don't put the 'images' directory in the 'src' folder, put it at the top level of the project (like 'bin' and 'META-INF'). Make sure you update the 'build.properties' to include the images folder in the build.
The path for the image is relative to the plug-in root so it will be images/EmailTemplatesOutput/assests/icon_4.png (assuming you move the images directory).
The URL returned by FileLocator.find uses an Eclipse only scheme so can only be used by Eclipse APIs. You can convert the URL to a normal file URL by adding:
URL fileURL = FileLocator.toFileURL(url);
This may cause Eclipse to unpack your plug-in to a temporary location to access the files.

Related

Relative filepath to access resources

I am quite new to Eclipse 4, RCP and SWT and I am stuck on this issue :
I want to access image resources from code with a relative filepath. Problem is that the default location ./ is set to my home directory /home/name/ (I'm using Ubuntu). I have found that by creating a new File and printing its CanonicalPath.
I am used to having the default location set to the project directory, such as /home/name/workspace/project/, which is, from what I've seen so far, the default behavior in Eclipse / java programs.
I would like to keep this behavior because it seems more reliable to me (after deployment for example).
Note: I have tagged e4, rcp and swt because I'm not sure which one causes the difference.
In an Eclipse plugin (including RCP code) you should use the FileLocator class to find resources within the plugin.
You open a resource as a stream:
Bundle bundle = ... plugin bundle
IPath path = new Path("path relative to plugin root");
InputStream is = FileLocator.openStream(bundle, path, true);
You can also use FileLocator.find:
URL url = FileLocator.find(bundle, path, null);
URL fileUrl = FileLocator.toFileURL(url);
Don't forget to include your resources directory in the build.properties.
If your image is not in the plugin you can't really use relative paths.
In an e4 plugin you can inject the Bundle using #OSGiBundle:
#Inject
#OSGiBundle
Bundle bundle;
(#OSGiBundle requires a dependency on the org.eclipse.e4.core.di.extensions plugin).

when I use getInstance of itext for image it is redirecting to different location [duplicate]

I'm doing the letter generation with iText (pdf/rtf) in java servlet and got a problem with accessing images. The images are in the WebContent/images folder. When I run it in a local server and pointing the full path of images directory (c://eclipse/myproject/WebContent/images/letterHead.jpg) its working, but it fails running on the server with the directory ("WebContent/images/letterHead.jpg").
The project is being deployed as a WAR on a tomcat server, thus ending up with an address similar to
http://someserver:8081/projectName/someJSP.jsp
I don't understand how to reference the images relatively in this environment, and any help would be much appreciated.
Here is my code
Image imghead = Image.getInstance("WebContent/images/letterHead.jpg");
imghead.setAbsolutePosition(35,770);
imghead.scaleAbsolute(125, 42);
document.add(imghead);
You should never use relative paths in java.io stuff. You will be dependent on the current working directory which is in no way controllable in case of a webapplication. Always use absolute disk file system paths. Thus, c:/full/path/to/file.ext.
You can use ServletContext#getRealPath() to convert a relative web path to an absolute disk file system path. The relative web path is rooted on the public webcontent folder which is in your case thus /WebContent. So, you need to replace the first line of above code by:
String relativeWebPath = "/images/letterHead.jpg";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
Image imghead = Image.getInstance(absoluteDiskPath);
// ...
Following piece of code may help you...
String path = request.getContextPath();
String split_path[] = path.split("/");
path = request.getRealPath(split_path[0]);
String imagePath="\\resources\\images\\letterHead.jpg";
Image image = Image.getInstance(path+imagePath);
Following code can be used to access image path inside a java class.
URL imageUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
Image img=Image.getInstance(imageurl+"../../../some/images/pic.gif");
So I had the same problem, I wanted to add an image to the pdf but through a relative path, so when I made a executable program it would work in any computer, even if it had or not the image saved.
I found a way where you add the image as a resource of your application. You need to put the image inside the src directory or the dir of the class where you call it and then you can use the following code:
Image image = Image.getInstance(yourClassName.class.getResource("/yourImageName")));
If you put the image inside a folder then, obviously, you'll need to add the path through the folders name ("folderName/yourImageName"). Hope it helps!
For Kotlin, if the image is in the resources folder, you can also try the following:
Image.getInstance(this::class.java.classLoader.getResourceAsStream("images/your_image.png").readBytes())
Resources
Image foto = Image.getInstance(this.getClass().getResource("/static/img/gobierno.jpg"));

eclipse plugin load file

i have created a plugin which can load the image from the icons folder as follow:
Image image = ImageDescriptor.createFromURL(
FileLocator.find(bundle, new Path("icons/image.gif"), null))
.createImage();
It worked fine.
Similarly I have created a folder called "assets" inside eclipse plugin project, & created a file called "Temp.txt" inside it.
But I am unable to find a way to load it into my java class. Kindly help me out in getting through this.
Use:
URL url = FileLocator.find(bundle, new Path("assets/Temp.txt"), null);
URL fileUrl = FileLocator.toFileURL(url);
File file = new File(fileUrl.getPath());
... read the file in the usual way

Getting file path from file name located in a directory in java

I have some jasper reports file inside a directory called "reports" which is located 2-3 level at the bottom of the "src" package in eclipse IDE. How could get the related file paths inside "reports" folder. I don't want to get the paths like "C:\Users\Kara\workspace\MyProject\src\main\resources\reports\someFile.jrxml". I tried to get them by following code but it didn't give me the real path.
File testFile = new File("someFile.jrxml");
String absPath = testFile.getAbsolutePath();
If the reports are alway in your classpath (which they appear to be, assuming a standard Maven directory layout), use Class.getResource()/ClassLoader.getResource() to get a URL pointing to your report file. You can use this URL to extract the absolute path to your report file.
URL reportUrl = getClass().getResource("reports/someFile.jrxml");
You can try this:
URL url = getClass().getResource("/reports/someFile.jrxml");
url.getPath();

Image path formatting

I'm trying to put an image to pdf file in my spring application. The code is
URL imageUrl = getClass().getResource(LOGO_PATH);
Image logo = Image.getInstance(imageUrl);
I have the image in source packages in com.application.pdf -package. How do I reference to LOGO_PATH then?
private static final String LOGO_PATH = "/src/java/com/application/pdf/logo.gif";
gives me null pointerexception.
You are tried to find resource in src folder, but in runtime you have to get resource from other folder, compile folder ( for example resource ) or from jar file, try to change your path to compile folder and I think all will be ok.
UPDATE
Could you provide structure of your application and I will help you to write correct path?

Categories

Resources