Can't find resource bundle - java

I'm trying to load a properties files, but i keep getting this error:
Exception in thread "main" java.util.MissingResourceException: Can't
find bundle for base name D:\bdtej04694\Mis
documentos\NetBeansProjects\SMS_Clientes_Menores\dist\lib\help.properties,
locale es_VE at
java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
I'm trying to making this
private static PropertyResourceBundle slInfo = null;
//and in another method i'm doing this
String directory = System.getProperty("user.dir");
slInfo = (PropertyResourceBundle)ResourceBundle.getBundle(directory+"\\dist\\lib\\help.properties");
//I put it on a different folder, just in case i want to make changes in the connection strings inside the properties files, without build the project again in netbeans
I searched on the web (and obviously, in this page), but i can't find an aswer to fit my problem
Thanks in advance

its seems you have problem with path of you properties file
D:\bdtej04694\Mis documentos\NetBeansProjects\SMS_Clientes_Menores\dist\lib\help.properties
try to avoid space in path of properties file, you have one here Mis documentos
regarding you comment :
change your path like below
String tempPath = directory+"\\dist\\lib\\help.properties";
String finalPath = tempPath.replace("\\", "/");
and use this finalPath

Related

How to get the path of the jar file instead of the path of the running class?

I need to know the location of my jar file , so I tried
String path = MigrationsApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
And this returned
file:/C:/Users/fabio/OneDrive/Ambiente de Trabalho/MigrationTool/colbi-migration-tool/target/migrations-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
Although I think I am capable of getting the location of my jar by doing a split by ! is there a better solution for this?
The final directory that I want in this specific case is target , so can after the split can I use the method getParent?

How to know absolute path to a properties loaded by resourceBoundle

I'm loading a properties using below code:
ResourceBoundle boundle = ResourceBundle.getBundle("file")
I want to know the absolute path of the loaded file. For example if I execute this code in a web application in webapps folder of a tomcat I want to obtain:
c:\tomcat8\webapps\example\WEB-INF\classes\file.properties.
How can I know this path?
I have 'resolved' my problem with this post 'Getting current working directory'. For my problem I only need this, once I know the working directory I can find the absolute path
public class Test {
public static void main(String... args) throws Exception {
URL location = Test.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
}}
I think that perhaps this solution does not cover all the situations, but it is enough for me.
You can't get the physical location from a ResourceBundle, but if you know the path that was used to load it, then you can find out the physical location of that. Like this:
String resourceToLoad = "file";
URL url = this.getClassLoader().getResource(resourceToLoad);
ResourceBoundle bundle = ResourceBundle.getBundle(resourceToLoad);
The url will have the physical location of the resource -- at least most of the time.
Remember that a ResourceBundle can also be backed by a Java class rather than something like a properties file. ClassLoader.getResource doesn't understand the mapping between ResourceBundle and Java classes. If you want to be able to support those, you'll have to implement a similar algorithm to what ResourceBundle does in order to search for classes that match it's scheme.

Java access files in jar causes java.nio.file.FileSystemNotFoundException

While trying to copy some files in my jar file to a temp directory with my java app, the following exception is thrown:
java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
at com.sora.util.walltoggle.pro.WebViewPresentation.setupTempFiles(WebViewPresentation.java:83)
....
and this is a small part of my setupTempFiles(with line numbers):
81. URI uri = getClass().getResource("/webViewPresentation").toURI();
//prints: URI->jar:file:/C:/Users/Tom/Dropbox/WallTogglePro.jar!/webViewPresentation
82. System.out.println("URI->" + uri );
83. Path source = Paths.get(uri);
the webViewPresentation directory resides in the root directory of my jar:
This problem only exits when I package my app as a jar, debugging in Eclipse has no problems. I suspect that this has something to do with this bug but I'm not sure how to correct this problem.
Any helps appreciated
If matters:
I'm on Java 8 build 1.8.0-b132
Windows 7 Ult. x64
A FileSystemNotFoundException means the file system cannot be created automatically; and you have not created it here.
Given your URI, what you should do is split against the !, open the filesystem using the part before it and then get the path from the part after the !:
final Map<String, String> env = new HashMap<>();
final String[] array = uri.toString().split("!");
final FileSystem fs = FileSystems.newFileSystem(URI.create(array[0]), env);
final Path path = fs.getPath(array[1]);
Note that you should .close() your FileSystem once you're done with it.
Accepted answer isn't the best since it doesn't work when you start application in IDE or resource is static and stored in classes!
Better solution was proposed at java.nio.file.FileSystemNotFoundException when getting file from resources folder
InputStream in = getClass().getResourceAsStream("/webViewPresentation");
byte[] data = IOUtils.toByteArray(in);
IOUtils is from Apache commons-io.
But if you are already using Spring and want a text file you can change the second line to
StreamUtils.copyToString(in, Charset.defaultCharset());
StreamUtils.copyToByteArray also exists.
This is maybe a hack, but the following worked for me:
URI uri = getClass().getResource("myresourcefile.txt").toURI();
if("jar".equals(uri.getScheme())){
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
try {
provider.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}
}
}
}
Path source = Paths.get(uri);
This uses the fact that ZipFileSystemProvider internally stores a List of FileSystems that were opened by URI.
If you're using spring framework library, then there is an easy solution for it.
As per requirement we want to read webViewPresentation;
I could solve the same problem with below code:
URI uri = getClass().getResource("/webViewPresentation").toURI();
FileSystems.getDefault().getPath(new UrlResource(uri).toString());

getResource with parent directory reference

I have a java app where I'm trying to load a text file that will be included in the jar.
When I do getClass().getResource("/a/b/c/"), it's able to create the URL for that path and I can print it out and everything looks fine.
However, if I try getClass().getResource(/a/b/../"), then I get a null URL back.
It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful.
The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library.
final String name = "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);
The path you specify in getResource() is not a file system path and can not be resolved canonically in the same way as paths are resolved by File object (and its ilk). Can I take it that you are trying to read a resource relative to another path?

How can I get relative path of the folders in my android project?

How can I get the relative path of the folders in my project using code?
I've created a new folder in my project and I want its relative path so no matter where the app is, the path will be correct.
I'm trying to do it in my class which extends android.app.Activity.
Perhaps something similar to "get file path from asset".
Make use of the classpath.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("path/to/folder");
File file = new File(url.toURI());
// ...
Are you looking for the root folder of the application? Then I would use
String path = getClass().getClassLoader().getResource(".").getPath();
to actually "find out where I am".
File relativeFile = new File(getClass().getResource("/icons/forIcon.png").toURI());
myJFrame.setIconImage(tk.getImage(relativeFile.getAbsolutePath()));
With this I found my project path:
new File("").getAbsolutePath();
this return "c:\Projects\SampleProject"
You can check this sample code to understand how you can access the relative path using the java sample code
import java.io.File;
public class MainClass {
public static void main(String[] args) {
File relative = new File("html/javafaq/index.html");
System.out.println("relative: ");
System.out.println(relative.getName());
System.out.println(relative.getPath());
}
}
Here getPath will display the relative path of the file.
In Android, application-level meta data is accessed through the Context reference, which an activity is a descendant of.
For example, you can get the source directory via the getApplicationInfo().sourceDir property.
There are methods for other folders as well (assets directory, data dir, database dir, etc.).
Generally we want to add images, txt, doc and etc files inside our Java project and specific folder such as /images.
I found in search that in JAVA, we can get path from Root to folder which we specify as,
String myStorageFolder= "/images"; // this is folder name in where I want to store files.
String getImageFolderPath= request.getServletContext().getRealPath(myStorageFolder);
Here, request is object of HttpServletRequest. It will get the whole path from Root to /images folder. You will get output like,
C:\Users\STARK\Workspaces\MyEclipse.metadata.me_tcat7\webapps\JavaProject\images
With System.getProperty("user.dir") you get the "Base of non-absolute paths" look at
Java Library Description

Categories

Resources