eclipse plugin load file - java

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

Related

Unable to get Image file URL from eclipse plugin project Bundle

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.

How to locate and execute a file with java

I am making a game with java as kind of a side project and am still new to the language. I was wondering how I could run a file when i didn't know the complete path, like if I were to send the game to a friend, and he has a different path location than me.
Thank you in advance for any help.
code I am currently using:
File file = new File("/Users/(my name)/Desktop/script1.vbs");
Desktop desktop = Desktop.getDesktop();
if(file.exists()) desktop.open(file);`
You could place the file (script1.vbs) in the project folder, that way the path would always be like this...
File file = new File("script1.vbs")
Place the file, not in the src or bin folder, but in the root folder.
Instead of
File file = new File("/Users/(my name)/Desktop/script1.vbs");
you should better use
File file = new File(System.getProperty("user.home"), "Desktop/script1.vbs");
See also the list of System Properties
Or you can use this, to locate your file in the resources folder:
File file = new File("classpath:com/company/script1.vbs");
"classpath:Route Of Source folders/Name and extension of the file"

How do I link my res folder to my java project

I just started a new Java Project and I'm working on Animations atm, my question now is how do I link my res source folder with the project so that I can read images via
img = ImageIO.read(getClass().getClassLoader().getResourceAsStream(name));
from that folder? Normally I they can only be read when they are in the
if we assume that your resource folder is called "resources":
Image image = Toolkit.getDefaultToolkit().createImage(yourclassName.class.getResource("/resources/..."));

Java - How To Set An Image Path Without Using The C:\\ Direcory

I created a java app and I put some images in it and even gave it an image icon as the desktop image, but when i made it a jar file, and put it on another pc, all images were gone. This is the image path :
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
I checked it online and i found out that the problem is that i got the file through the C:\\ directory, they said the image file should look like this :
File imageFile = new File("res/icon.jpg");
I tried this but it didnt work, I kept getting an error message like : file not found
This is my full code :
BufferedImage image = null;
try {
File imageFile = new File("C:\\Users\\Favour's Computer\\workspace\\Physics Calculator\\src\\res\\icon.jpg");
image = ImageIO.read(imageFile);
} catch(IOException e) {
e.printStackTrace();
}
setIconImage(image);
Please, i have been trying to solve this for weeks, do anyone know how i can solve this, please if you do, please help
The images should not be loaded from the file system, but should be bundled into the app, inside your jar.
If you put the image foo.png inside the jar, under the package com.bar.resources for example, you simply need to use
InputStream in = getClass().getResourceAsStream("/com/bar/resources/foo.png")
to load the image as an input stream.
That will use the class loader to load the image. So, during development, if you're using a standard IDE project, you just need to put the image file in the appropriate package in your source directory: the IDE will "compile" the file by copying it to the same directory as the generated .class files.
If you're using a standard Maven/Gradle project, then it needs to be put in the appropriate package under src/main/resources.
Problem is you dont have res folder in your project, first of create a folder a "res" by Right Clicking on project and place the image icon.jpg image in that "res". and
use : `File imageFile = new File("res/icon.jpg");`// To retrieve image to your project.
You cannot retrieve the image from your local system, because it is not attached with your projects workspace.
There are two options....
1)Either make res a "source folder" . You will know this if you are using Eclipse.
Then you can use like
ImageIO.read(new File("res/icon.jpg"));
2) If res is a normal folder, you will have to use. In this case res will be considered as a package
ImageIO.read(new File("src/res/icon.jpg"));

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