GWT resourcebundle can't find classpath - java

So I have a GWT project and on the server side I am trying to load a properties file into a resource bundle, but am failing. I keep getting a missing resource exception because it can't locate the file but every option I've tried doesn't work. I've tried placing the config.properties file in war/config/config.properties as well as src/config/config.properties (so 2 copies) and I've tried calling it the following ways
TestGWTProject.war.config.config.properties
TestGWTProject.war.config.config
TestGWTProject.src.config.config.properties
TestGWTProject.war.config.config
src.config.config.properties
src.config.config
and my current call is this
ResourceBundle bundle = ResourceBundle.getBundle("config");
yet nothing works. Ideally, I'd like to keep it in the war file, but if it has to stay in src no biggie, but I'd like to just be able to find it at this point. What am i overlooking?

If you put your properties in the src folder (as I think you should) you need to give ResourceBundle a class-like qualified name, so that when application gets compiled, it can find the properties file in the classes folder.
In my case, for ex., I place them in my source folder, inside a package. Like this:
src/package/subpackage/subsubpackage/propertiesFile.properties
When I want to load the properties, I use this as the qualified name:
package.subpackage.subsubpackage.propertiesFile
This is the same way you would give your application a full qualified name for a class. The key here is that the properties file is getting copied to the classes folder inside the WAR. So your mistake was telling the ResourceBundle to look into the src folder.

Related

Whats the best way to specify a path to read from class-path even if project was a jar

I am trying to read a Properties file in a maven nature project using the Properties.load(); I am specifying a path as a string ex. "./someFolder/file.properties",
but when I try to use my project as dependency in other projects I am forced to copy those files to the other project , simply because the "." means current directory.
Is there a way to specify a path so it will always be valid despite where I am calling it from ? ,
I have tried using the MyClass.class.getClassLoader().getResourceAsStream() but I am having trouble using it , it worked sometimes and failed other times.
There are lots of misconceptions in your question.
"." means classPath
No. When used inside a filesystem path (i.e. a path passed to the constructor of a File, or FileReader, or FileInputStream), "." means the current directory.
When used in a resource path (i.e. passed to Class[Loader].getResource[AsStream]()), it's invalid.
The trick is to carefully read the documentation.
getResourceAsStream() expects a /-separated path.
When using ClassLoader.getResource[AsStream](), this path always starts from the root of the classpath. So you would pass a path looking exactly like a fully qualified class name, except the dots would be replaced by slashes. So, com/foo/bar.properties looks for a resource named bar.properties, in the package com.foo.
When using SomeClass.class.getResource[AsStream](), either the path starts with a /, and the path starts from the root of the classpath, or it doesn't, and it starts from the package of SomeClass. So, if SomeClass is in the package com.foo, using /com/foo/bar.properties is equivalent to using bar.properties.
It's hard to tell what you're doing wrong, since you're not providing any detail. But you really need to understand the difference between opening a file on the file system, and reading a resource loaded by the class loader. Sometimes, the resources just happen to be loaded by the class loader from the filesystem, because the classpath happens to contain directories, and not just jar files.
I noticed that my problem was that I had my properties files in the project path itself, and that the ClassLoader.getResource[AsStream](); looks is the target/classes folder, and that I didn't have the resources folder in my project.
I solved it my adding the resources folder to my build path and adding my files in the src/main/resources as the following src/main/resources/foo/bar.properties and loading it by SomeClass.class.getClassLoader().loadResourceAsStream("foo/bar.properties");.

Overriding a resource contained in a jar file

I am running a tool (a virtual globe) through code from a jar file. The code reads a resource (an XML file) to provide some configuration options, using syntax like this, in a method of class Config:
URL localURL = Config.class.getResource("Config.xml");
I would like to provide my own Config.xml file with settings that override those in the file contained in the jar file.
I am not clear about how I can do this. I understand that the getResource() method explores the classpath to find the resource. So I thought of this:
- putting a copy of the file with my own settings in a specific directory
- putting this directory in front of the classpath
But to no avail: the getResource() still always loads the resource from the jar file.
I must be missing something ...
I tried removing the Config.xml file from the application jar. That fails: the application fails because getResource() returns null. It seems to me like
Config.class.getResource("Config.xml")
only looks for resources inside the jar file that contains class Config, whereas I thought it was looking in the classpath.
OK, got it. The issue is this: my Config class is really in a package, i.e. vis.globe.Config.java, so getResource("Config.Xxml") really looks for a file called vis.globe.Config.xml.
Therefore, with a classpath such as "../config:../jar/appl.jar", it will look for file Config.xml, not in ../config, but in ../config/vis/globe.
So the solution was to create a sub-directory structure in ../config that reflects the fully qualified name of class Config.java, i.e.
../config/vis/globe/Config.xml
You could always look to do something programmatically. So for example you would first search for a file named UserConfig.xml and if it was not found fall back on the file named Config.xml which would be found in the jar file.

cannot locate properties file in java path

My code cannot locate the .properties file where i have stored login information.
I have put the file in the src folder to make sure it compiles, and it does correctly.
below is the current location of the file and how i am trying to access it.
I have tried various different paths but no luck.
Change your code;
ResourceBundle bundle = ResourceBundle.getBundle("Selenium/readme");
to
ResourceBundle bundle = ResourceBundle.getBundle("readme");
You don't compile a .properties file, you use it as is.
If you use a FileInputStream it will use the working directory which is set in your Run configuration (most likely the top directory)
But you are loading it as a resource which means it must be in your class path. The simplest thing to do is to create a sub-directory for your configuration and add this to your programs class path.
Read properties file like:
ResourceBundle.getBundle("src/properties/readme.properties"); //Or simply "properties/readme.properties"
Put readme.properties under src/properties directory.
you can use this.getClass().getResourceAsStream("readme.properties");
for more information read:
I see that the .properties file is not inside the src folder. Also check the build path of your project.It will show you the src folders and the output folders location. Once you build the project using eclipse build project option, make sure your properties file is now available in the output folder.

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

Java Properties File not loading

I need a configuration file in my program to store some information, I saw some examples of properties files and was trying to use them but whenever I try this second line on NetBeans i get 'Package sortConfig doesn't exist' '<identifier> Expected' and 'Illegal start of type'.
The problem is I already saw some 10 examples all doing this in the same way, and I have no clue to what's going on.
Properties sortConfig = new Properties();
sortConfig.load(this.getClass().getClassLoader().getResourceAsStream("sortConfig.properties"));
Any help would be appreciated
my .java classes and my properties file are on the src, inside the same package folder
It looks like you do not have the fully qualified path to your properties file. You can get to it in 2 ways:-
Using java.util.ResourceBundle:
ResourceBundle bundle
=ResourceBundle.getBundle("br.com.example.sortConfig");
//.properties is implied
or
Using ClassLoader.getResouceAsStream:
sortConfig.load(this.getClass().getClassLoader().getResourceAsStream("br/com/example/sortConfig.propertie"));
For a good tutorial on how to load properties files resources check out this link.
An alternative could be to use this.getClass().getResourceAsStream() which accepts relative pathnames (relative to the package your class is in, that is), so you could simply write
sortConfig.load(this.getClass().getResourceAsStream("sortConfig.properties"));
This is useful when you specifically want to rely on your class and properties file being in the same package. (So when you move one during a refactoring, you'll have to move the other too.)

Categories