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.
Related
I have a NetBeans project that uses the GSON library. I've tried including the GSON.jar file without requiring future users to separately download it. However it doesn't seem to work. The project looks for the file from the relative path of my computer so the file isn't found on another user's computer. Is there a way to include GSON.jar and "Export to Zip" and keep the reference in the project itself? I'm lost!
Thank you
Exporting a Project to ZIP zips up the project folder only, and not anything outside of the folder, including dependencies. If you include the GSON.jar file in the project folder, then the JAR file will be included in the .ZIP file. It's a good practice anyway since NetBeans will use a relative classpath and thus if you move the project itself NetBeans won't give you an error message when loading the project.
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.
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);
I have searched a lot and tried several ways to do this, but am stumped. I am writing a desktop app (though I suppose it could also run in a browser) that allows an Android programmer to edit all of their dimens.xml files at once. I have created my own images to use in ImageViews as buttons. I am using different methods to access these images:
Some are referenced in the FXML file, like <Image url="#Icons/ic_launcher.png" />. The path is "src/Icons/". The path of the FXML file is "src/application/xxx,fxml".
Some are referenced in the css file, like "-fx-image:url("QuestionMark.png");". The css file and png file are in the same directory "src/MessageBox/"
Some are changed dynamically at runtime:
ImageView mButtonIcon = new ImageView("/insert_item_above.png");
The path of the image: src/
The path of the class: src/ContextMenuButton/
The above only works in Netbeans 7.4. None work when I run the app in Eclipse.
If I go to the dist/ directory and run it from any of the 3 methods, I see my images.
If I move the dist/ folder somewhere else, the only way I can get it to work is if I copy the src/ folder to the same directory and delete everything but the png and css files. So, I end up with:
+ F:/AndroidDimens
+ dist
xxx.jar
+ src
insert_item_above.png
+ Icons
ic_launcher.png
+ MessageBox
QuestionMark.png
So, the jar file has modified all of the paths to be relative to the src/ folder. My goal is to make the paths relative to where the jar file is. I tried to place the images where I would not need project related paths. But it must have made the paths something like "../src/MessageBox/QuestionMark.png" in the jar (relative to the project's dist/ folder).
Is there any way to fix this? Ideally, I would like all images to be in one directory. Then I could zip that directory, and someone else could just unzip it and run the app.
Thanks!
EDIT
Thanks to #jewelsea (in chat), I found that the problem was due to having an older version of JDK 7u13 installed with the latest one needed for JavaFX 2.2. Deleting the old version, and updating global variables that referenced it, solved the problem. No changes were needed to the default project settings.
Packaging Advice
Package all of your application's runtime class files and resources (fxml, css, png, etc) in the application jar file using the JavaFX packaging tools.
Using the JavaFX packaging tools is what NetBeans 7.4 does automatically during it's build process for JavaFX application projects.
Eclipse and other build environments will not use the JavaFX packaging tools automatically. I believe, if you use Eclipse with the recommended e(fx)clipse extension toolset for JavaFX development, then that toolset will, through its UI, provide you with the ability to use the JavaFX packaging tools to package your application.
There are 3rd party packaging alternatives for JavaFX such as the JavaFX Maven Plugin or the JavaFX Gradle Plugin which will also package your application correctly.
Whatever packaging tool you choose, test the packaging process by unzipping the files from your resultant jar and checking that all of the resource files (fxml, css, png, properties etc) are where you expect them to be in the jar's internal directory structure. This unzipping process is just a developer sanity check, you don't need to ask your end users do perform such an extraction.
Your end users can run your application as either an installed native application (JavaFX term self-contained application) or as a click to execute jar file (JavaFX term standalone program) and all of your application's resources will automatically be available from the packaged application, with no additional work required by the user.
Resource Access Advice
I advise not referring to a src path in your code (as you won't have a src path inside your distribution jar), css or fxml files, but instead refer to those paths relative to the root of the distribution jar or your JavaFX application class. For example, to load a scene style sheet in a JavaFX Application subclass, use a form as recommended by the JavaFX deployment guide - 3.3.4 Loading Resources:
scene.getStylesheets().
add(this.getClass().getResource("my.css").toExternalForm());
I am developing a application in Netbeans7.1. I am facing one problem to add new jar file from the app after building the app.
As i know, when we build the project in Netbeans that will create a "jar" file and "lib" directory (which has all the libraries those are being used in the application) into the "dist" directory.
The problem is, I have to add new jar from my application into /lib/ directory after building the project. So that jar will be used in the application.
How should i do this?
If you have a successful build then it means the jar you wants to add is required at runtime not at compile time otherwise it wouldn't have compiled.
And if above is the case adding jar is straightforward.
Add the jar file to lib folder.
Now open you applications jar with some rar software like winrar.
Goto the META-INF folder open the MANIFEST.MF file and append the class path with lib/new_jar_file.jar.
Bingo..You are done.
Here is a screenshot of MAINIFEST.MF file and red box shows where to add the above mentioned lines.