Where is application-scope location of resources in Eclipse RCP? - java

I am wrapping some 3rd party application into Eclipse plugin.
This application uses some data files in "current" directory, i.e. directory where application ran from.
Initially I put all of theses files at first level in Eclipse plugin project and can access these files via
bundle.getEntry("./conf/conf.xml")
unfortunately, this code creates strange URLs like
bundleentry://88.fwk1692538903/./conf/conf.xml
Why is it so? Is it possible to put all application-scoped files into some global location and refer it from a plugin?

Use org.eclipse.core.runtime.FileLocator to convert the bundleentry URL to a normal file URL:
URL bundleURL = bundle.getEntry("...");
URL fileURL = FileLocator.toFileURL(bundleURL);
Note: If the plugin is in a jar rather than expanded during install the URL returned may reference a temporary location in the metadata where the plugin has been expanded.

Related

stop eclipse from deleting folders inside "target"

I am developing a maven project with Spring mvc application with Eclipse. I have some cucumber-selenium test case to check the some behavior of app. I put the chromDriver.exe in the path target\classes\Driver.
When i want to run the application it complains about:
The project was not built due to "Could not delete '/CyberMall/target/classes/Driver'.". Fix the problem, then try refreshing this project and building it since it may be inconsistent CyberMall Unknown Java Problem
It seems that, it tries to delete the Driver folder inside the target, and it fails, so it cannot build the application.
So, is there any way to ask Eclipse to stop deleting the Driver folder?
The reason I have put the driver in this path is that, i can easily access to it using the below code.
File file = new File(CyberMallApplication.class.getClassLoader().getResource("Driver/chromedriver.exe").getFile());
String driverPath=file.getAbsolutePath();
System.out.println(driverPath);
System.setProperty("webdriver.chrome.driver",driverPath);
If i put it into the resources, i don't know how to access it?
I think it's not a good practice to put a file that should be used in the targer folder.
The target folder should be clean at every maven install so the content is deleted.
You can put your file in the resources folder insted.
Take a look at that link : https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
The target directory is used to house all output of the build.
The content of the resource directory will be put inside WEB-INF/classes. So you can adapt your resources folder to have your folders like you already have.
resources/Driver/chromedriver.exe
You should put chromedriver.exe to src/main/resources and set relative path in the system properties
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");

Plugin Development, where to save user data?

I am developing a plugin for Eclipse. When the user runs the application for the first time, calibration takes place. This is where user data is taken and then printed out to text files, so that when used again, the system will remember their settings.
I was wondering where is the best place to store these text files on a user's system?
There are different options with different scopes:
per installation: see the post, create a unique folder under the mentioned path and store the files there
per user: create a unique folder under the users home directory (System.getProperty("user.dir")) and store the files there
per workspace: Plugin::getStateLocation returns a path that is unique to the given plug-in for each workspace. Store your files there. Plugin is an abstract class that is implemented by the Activator or your plug-in. If you don't have such class yet, create it by specifying the Activator in the manifest editor. Alternatively you can use Platform::getStateLocation() if you have a reference to the Bundle that represents your plug-in.
A common place to store such data is the
AppData\Local\YourPluginName
directory.
You may also use the
eclipse/plugins/YourPlugin
directory.
Your plugin can store data in the workspace metadata in the plugin 'state location'
Use something like:
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath stateLoc = Platform.getStateLocation(bundle);
stateLoc will be a directory where your plugin can store whatever it likes. The location will normally be '.metadata/.plugins/your plugin id' in the workspace.

How to retrieve .properties?

Im developing desktop java application using maven.
I got a *.properties file that I need to retrive during execution (src/resources/application.properties).
The only thing comes to my mind is to use:
private Properties applicationProperties;
applicationProperties.load(new BufferedInputStream(new FileInputStream("src/resources/application.properties")));
This would work if I run my application directly from IDE.
I want to to keep outpout hierarchy clear, so I set maven to copy resources folder dircetly to target folder (which is a basedir for the output application). This way application.properties file won't load (since I have target/resources/application.properties but not target/src/resources/application.properties).
What is the best way to manage resources so they work both when I debug from IDE and run builded jar file directly?
Don't expect files to be in the src folder - it doesn't exist at runtime. The properties files go to /bin. But don't rely on that either. Because FileInputStream takes absolute paths only.
When you need a classpath-relative path, use:
InputStream is = YourClass.class.getResourceAsStream("/a.properties")`
(maven sends files from /src/main/resources to the root of the classpath)
You should load the property file from the classpath rather than from an explicit file system location:
applicationProperties.load(new BufferedInputStream(this.getClass().getResourceAsStream( "/application.properties" );
As long as your IDE is configured to include the resources directory on your classpath (this should be the default with Maven), then this will work whether you're running within the IDE or not since maven will copy the resources to the right place when packaging your archive.

Java (maven web app), getting full file path for file in resources folder?

I'm working with a project that is setup using the standard Maven directory structure so I have a folder called "resources" and within this I have made a folder called "fonts" and then put a file in it. I need to pass in the full String file path (of a file that is located, within my project structure, at resources/fonts/somefont.ttf) to an object I am using, from a 3rd party library, as below, I have searched on this for a while but have become a bit confused as to the proper way to do this. I have tried as below but it isn't able to find it. I looked at using ResourceBundle but that seemed to involve making an actual File object when I just need the path to pass into a method like the one below (don't have the actual method call in front of me so just giving an example from my memory):
FontFactory.somemethod("resources/fonts/somefont.ttf");
I had thought there was a way, with a project with standard Maven directory structure to get a file from the resource folder without having to use the full relative path from the class / package. Any advice on this is greatly appreciated.
I don't want to use a hard-coded path since different developers who work on the project have different setups and I want to include this as part of the project so that they get it directly when they checkout the project source.
This is for a web application (Struts 1.3 app) and when I look into the exploded WAR file (which I am running the project off of through Tomcat), the file is at:
<Exploded war dir>/resources/fonts/somefont.ttf
Code:
import java.io.File;
import org.springframework.core.io.*;
public String getFontFilePath(String classpathRelativePath) {
Resource rsrc = new ClassPathResource(classpathRelativePath);
return rsrc.getFile().getAbsolutePath();
}
In your case, classpathRelativePath would be something like "/resources/fonts/somefont.ttf".
You can use the below mentioned to get the path of the file:
String fileName = "/filename.extension"; //use forward slash to recognize your file
String path = this.getClass().getResource(fileName).toString();
use/pass the path to your methods.
If your resources directory is in the root of your war, that means resources/fonts/somefont.ttf would be a "virtual path" where that file is available. You can get the "real path"--the absolute file system path--from the ServletContext. Note (in the docs) that this only works if the WAR is exploded. If your container runs the app from the war file without expanding it, this method won't work.
You can look up the answer to the question on similar lines which I had
Loading XML Files during Maven Test run
The answer given by BobG should work. Though you need to keep in mind that path for the resource file is relative to path of the current class. Both resources and java source files are in classpath

What is the proper way to use ResourceBundle.getBundle() in an Android application?

I want to know how I can use ResourceBundle.getBundle() in an Android application given that I use it in my Java applications. For example:
I have a properties file, "MyOrg.properties", which I've included in a JAR file named "MyOrg.jar". The path information in the JAR file associated with "MyOrg.jar" is "myorg\" (this is on a Windows system). I build my Java application using "myorg.jar" and note that the following line of code works as expected, which is that it loads the file "MyOrg.properties" from "MyOrg.jar" as a java.util.ResourceBundle:
ResourceBundle resources = ResourceBundle.getBundle( "myorg.MyOrg" );
Next, I place a modified copy of the file "MyOrg.properties" on the file system in the directory "c:\myorg", which is on my Java application's class path. I now rerun my Java application and note that the Resource.getBundle() returns, as expected, a bundle for the modified copy of "MyOrg.properties" (the one that is on the file system).
The behavior I've just described is what I would like to be able to accomplish with an Android application. However, ResourceBundle.getBundle() throws a MissingResourceException; it fails to find "MyOrg.properties" in either the JAR file or as a stand-alone file.
So I have two questions:
1) - Is it possible for an Android application to retrieve a resource from a JAR file using ResourceBundle.getBundle() (or any other way for that matter)?
2) - Is it possible for an Android application to retrieve a resource from a file using ResourceBundle.getBundle()?
Regarding 2): I'm using the nbandroid plugin with NetBeans 6.7.1 and I've placed copies of "MyOrg.properties" on the file system as follows prior to building my Android application:
MyProject
-- build
-- classes
myorg (directory contains "MyOrg.properties")
...
src
myorg (directory contains "MyOrg.properties")
you need to make sure the properties file makes it to the .apk file. your best bet is probably in res/raw/ or assets/. see also PropertyResourceBundle's constructors (since it's easy to get hold of an InputStream).

Categories