Why do Properties load method failed in Eclipse plugin dev? - java

I'm trying to use Properties's load() method in my Eclipse plugin project. Since I wanna put the propertie file in a folder like this:
Pluging Project/
|
+----/src/...
+----/config/config.properties
+----/icons/...
+----META-IN/
+----build.properties
+----plugin.xml
And then I try code like this but failed:
Properties prop = new Properties();
InputStream inputStream = (InputStream) Activator.getDefault().getBundle().getEntry("/config/config.properties").getContent();
prop.load(inputStream);
This method receipt a input byte stream as parameter. And I'm pretty sure Activator.getDefault().getBundle().getEntry() returns a InputStream.
And if I put the propertie file in the same location of the calling class and use
InputStream inputStream = this.getClass().getResourceAsStream("config.properties");
It will go well.
So any hints?

The URL returned by Bundle.getEntry uses an internal Eclipse scheme and doesn't always support getContents(). You need to call org.eclipse.core.runtime.FileLocator.toFileURL() to convert it:
URL url = Activator.getDefault().getBundle().getEntry("/config/config.properties");
url = FileLocator.toFileURL(url);
InputStream inputStream = (InputStream)url.getContent();
Also make sure you have the config directory listed in the build.properties

Related

Java: Getting resource path of the main app instead of jar's

A lot has been discussed already here about getting a resource.
If there is already a solution - please point me to it because I couldn't find.
I have a program which uses several jars.
To one of the jars I added a properties file under main/resources folder.
I've added the following method to the jar project in order to to read it:
public void loadAppPropertiesFile() {
try {
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String resourcePath = this.getClass().getClassLoader().getResource("").getPath();
InputStream stream = loader.getResourceAsStream(resourcePath + "\\entities.properties");
prop.load(stream);
String default_ssl = prop.getProperty("default_ssl");
}catch (Exception e){
}
}
The problem (?) is that resourcePath gives me a path to the target\test-clasess but under the calling application directory although the loading code exists in the jar!
This the jar content:
The jar is added to the main project by maven dependency.
How can I overcome this state and read the jar resource file?
Thanks!
I would suggest using the classloader used to load the class, not the context classloader.
Then, you have two options to get at a resource at the root of the jar file:
Use Class.getResourceAsStream, passing in an absolute path (leading /)
Use ClassLoader.getResourceAsStream, passing in a relative path (just "entities.properties")
So either of:
InputStream stream = getClass().getResourceAsStream("/entities.properties");
InputStream stream = getClass().getClassLoader().getResourceAsStream("entities.properties");
Personally I'd use the first option as it's briefer and just as clear.
Can you try this:
InputStream stream = getClass().getClassLoader().getResourceAsStream("entities.properties")

Read files from different Jars in java

I want to get values from properties file inside JARs. I have two Jar files. both of them in class-path.
1- lib/seed.jar (has common.properties).
2- lib/span.jar (has common.properties).
Both Jars has same name of properties file but with different value
When I use the following:
InputStream input = getClass().getResourceAsStream("/common.properties");
It will read only from the first jar, but I will be unable to read value from the second jar. How to let my code be able to access the file in those jar?
This approach will be effective,use the getResources method,like:
Enumeration<URL> resources = Main.class.getClassLoader().getResources("client.xml");
while (resources.hasMoreElements()){
URL url = resources.nextElement();
File file = new File(url.getFile());
FileInputStream input = new FileInputStream(file);
System.out.println(input);
}
And pay attention to the argument of the getResources is not be necessary start with /,if not,the method will not get correct file.

Read Properties file in java

How do I give absolute path to the properties file.
autoamtion_environment_properties = new Properties();
InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(("C:\\automation_environment.properties"));
This is giving a null pointer exception.
If I have this file in project root folder it works, but I need to access it from outside. Any idea what needs to be done?
Thanks.
The file has to be in the CLASSPATH for it to work. Your IDE papers over the difficulty for you, but you'll need to know what you're doing when you don't have the crutch. Include the directory where the .properties files live in your CLASSPATH.
If you know the full path for the file, you can use FileInputStream class
InputStream iStream = new FileInputStream(new File("C:\\automation_environment.properties"));
Otherwise, please refer to this answer https://stackoverflow.com/a/676273/176569
Why not use a FileInputStream instead of all that crazy Thread stuff?
InputStream in = new FileInputStream(new File("C:\\automation_environment.properties"));
http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html
I would try to set \ to / instead like:
InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(("C:/automation_environment.properties"));

JAVA: FileInputStream and FileOutputStream

I have this strange thing with input and output streams, whitch I just can't understand.
I use inputstream to read properties file from resources like this:
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream( "/resources/SQL.properties" );
rop.load(in);
return prop;
It finds my file and reds it succesfully. I try to write modificated settings like this:
prop.store(new FileOutputStream( "/resources/SQL.properties" ), null);
And I getting strange error from storing:
java.io.FileNotFoundException: \resources\SQL.properties (The system cannot find the path specified)
So why path to properties are changed? How to fix this?
I am using Netbeans on Windows
The problem is that getResourceAsStream() is resolving the path you give it relative to the classpath, while new FileOutputStream() creates the file directly in the filesystem. They have different starting points for the path.
In general you cannot write back to the source location from which a resource was loaded, as it may not exist in the filesystem at all. It may be in a jar file, for instance, and the JVM will not update the jar file.
May be it works
try
{
java.net.URL url = this.getClass().getResource("/resources/SQL.properties");
java.io.FileInputStream pin = new java.io.FileInputStream(url.getFile());
java.util.Properties props = new java.util.Properties();
props.load(pin);
}
catch(Exception ex)
{
ex.printStackTrace();
}
and check the below url
getResourceAsStream() vs FileInputStream
Please see this question: How can I save a file to the class path
And this answer https://stackoverflow.com/a/4714719/239168
In summary: you can't always trivially save back a file your read from the classpath (e.g. a file in a
jar)
However if it was indeed just a file on the classpath, the above answer has a nice approach

How to reference a file in Eclipse that is not in src

I'm trying to get a resource for MyBatis. The tutorial states that I will need the following in my Connection Factory:
String resource = "org/mybatis/example/Configuration.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
My directory structure is:
src/
com/
utils/
MyBatisConnectionFactory.java
config/
Configuration.xml
I am having troubles referencing the configuration file. I tried "config/Configuration.xml", "Configuration.xml" and "/config/Configuration.xml".
Anyone have a good idea for what to do?
You can add your config directory as a source-folder (right-click > build path > use as source folder).
Thus your configuration files will go on the root of the classpath and will be accessible via getClass().getResourceAsStream("/Configuration.xml")
Open up the file via the classpath using getResourcesAsStream() rather than Resources.getResourceAsReader() For example:
InputStream is = getClass().getClassLoader().getResourceAsStream(
"src/com/utils/Configuration.xml");
byte[] data = new byte[is.available()];
is.read(data);
is.close();
String fileContents = new String(data);

Categories

Resources