I am trying to access my resource files from my class path. I have stored all my resources in a folder called "config" which has been added to my build path.
Now the problem is, i am unable to access them. I have tried following options, but none of them seems to work. Everything returns null.
String resourceName = "/config/LPANewCommonModelSchema.xsd";
System.out.println(getClass().getClassLoader().getResource(resourceName));
System.out.println(getClass().getResource(resourceName));
System.out.println(Thread.currentThread().getContextClassLoader().getResource(resourceName));
String resourceName = "config/LPANewCommonModelSchema.xsd";
System.out.println(getClass().getClassLoader().getResource(resourceName));
System.out.println(getClass().getResource(resourceName));
System.out.println(Thread.currentThread().getContextClassLoader().getResource(resourceName));
String resourceName = "/LPANewCommonModelSchema.xsd";
System.out.println(getClass().getClassLoader().getResource(resourceName));
System.out.println(getClass().getResource(resourceName));
System.out.println(Thread.currentThread().getContextClassLoader().getResource(resourceName));
String resourceName = "LPANewCommonModelSchema.xsd";
System.out.println(getClass().getClassLoader().getResource(resourceName));
System.out.println(getClass().getResource(resourceName));
System.out.println(Thread.currentThread().getContextClassLoader().getResource(resourceName));
EDIT:
Somebody asked for hierarchy,
Thanks in advance,
Harsha
Try with this (assume the current calss is beside resources folder)
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("config/LPANewCommonModelSchema.xsd");
or this
InputStream in = this.getClass().getResourceAsStream("/config/LPANewCommonModelSchema.xsd");
Related
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")
I'm a beginner in programming and I have a class that requires a path to some folder in its constructor. For example:
SomeClass class = new SomeClass(c:/folder);
I need to get a String value of resources folder (only path to folder without specific file names). A method in that class will add a filename to path from constructor and do some operations with a specific file.
I'm trying to do this (bad code!), but just have npe:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("src/main/resources").getFile());
String path = file.getAbsolutePath();
So, the question is: how to get String value of resources folder?
What solved my problem:
File file = new File("src/main/resources");
String path = file.getAbsolutePath();
So, I got String path to resources folder.
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
Let's say I have a package: com.example.resources and inside it I have picture.jpg and text.txt - How can I use only the package path to identify the absolute filepath of picture.jpg and text.txt?
Is this correct?
File file1 = new File("/com/example/resources/picture.jpg");
file1.getAbsolutePath();
File file2 = new File("/com/example/resources/text.txt");
file2.getAbsolutePath();
"I'm trying to reference those files within the application so that when I deploy the application to different systems I won't have to reconfigure the location of each resource "
You don't want to use a File, which will load from the file system of the local machine, you want to load through the class path, as that's how embedded resources should be read. Use getClass().getResourceAsStream(..) for the text file and getClass().getReource(..) for the image
Your current path you're referencing looks like it should work, given the package name you've provided.
Take a look at this answer and this answer as some references.
I've figured out how to do it. Here is the code that I used:
public String packagePathToAbsolutePath(String packageName, String filename) {
String absolutePath = "File not found";
URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
File[] files = new File(root.getFile()).listFiles();
for (File file : files) {
if (file.getName().equals(filename)) {
absolutePath = file.getName();
return absolutePath;
}
}
return absolutePath;
}
This method was really useful because it allowed me to configure the resource paths within the application so that when I ran the application on different systems I didn't have to reconfigure any paths.
Is there a way for java program to determine its location in the file system?
You can use CodeSource#getLocation() for this. The CodeSource is available by ProtectionDomain#getCodeSource(). The ProtectionDomain in turn is available by Class#getProtectionDomain().
URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
File file = new File(location.getPath());
// ...
This returns the exact location of the Class in question.
Update: as per the comments, it's apparently already in the classpath. You can then just use ClassLoader#getResource() wherein you pass the root-package-relative path.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("filename.ext");
File file = new File(resource.getPath());
// ...
You can even get it as an InputStream using ClassLoader#getResourceAsStream().
InputStream input = classLoader.getResourceAsStream("filename.ext");
// ...
That's also the normal way of using packaged resources. If it's located inside a package, then use for example com/example/filename.ext instead.
For me this worked, when I knew what was the exact name of the file:
File f = new File("OutFile.txt");
System.out.println("f.getAbsolutePath() = " + f.getAbsolutePath());
Or there is this solution too: http://docs.oracle.com/javase/tutorial/essential/io/find.html
if you want to get the "working directory" for the currently running program, then just use:
new File("");