ClassLoader shows wrong path on Linux - java

I am using classLoader to load xml files located under /src/XMLS :
String m_path = "XMLS/file.xml"
ClassLoader cl = getClass.getClassLoader();
file f1 = new file(cl.getResource(m_path).getFile));
Running on windows it works fine but after export to jar and running it on Linux I get FileNotFoundException - /XMLS/file.xml.
I had tried this solutions and I dont think that the problem is in the read from the .jar file. any other ideas for what I am doing wrong?

This will not work for a resource inside a jar file, which is not a file on the filesystem. Instead, you need to use getResourceAsStream(), which returns an InputStream to use directly:
InputStream in = someClass.getClassLoader().getResourceAsStream("/XMLS/file.xml");

Related

FileNotFoundException while running as a jar

FileInputStream fstream = new FileInputStream("abc.txt")
is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.
class MyClass{
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
}
This code should be used.
And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .
So we should keep the abc.txt in com/myfolder this location.
If your file is packaged with your jar then you should to get information using getClass().getResource(url):
FileInputStream inputStream =
new FileInputStream(new File(getClass().getResource(/path/to/your/file/abc.txt).toURI()));
Else you need to create it always in the same path with your jar and you can get it like you do :
src/myJar.jar
src/folder/abc.txt
FileInputStream fstream = new FileInputStream("folder/abc.txt");
You can read here also :
How do I load a file from resource folder? and File loading by getClass().getResource()
You can use FileInputStream only when you actually have a file on the computer's filesystem. When you package your text file in the jar file for your program, it is not a file in the filesystem. It is an entry inside the jar file.
The good news is that it is even easier, in Java, to access the file this way: it is in your classpath, so you can use getResourceAsStream().
InputStream stream = getClass().getResourceAsStream("abc.txt");
If you have your classpath set up correctly, this will work regardless of whether it is a file in a directory (such as during development), or an entry in a jar file (such as when released).
It's because your working directory will probably be different under the two environments. Try adding the line
System.out.println(new File("abc.txt").getAbsolutePath());
to see where it is actually looking for the file.

Java Load From Reasource In Jar

I am having some difficulty loading files from resources in eclipse. In the IDE, loading works fine. In a compiled jar everything breaks.
This works for loading an image inside and outside of a jar:
ImageIO.read(this.getClass().getResourceAsStream("E.png"))
This works only in the IDE for a file object (I know it's messy):
File f = new File(Thread.currentThread().getContextClassLoader().getResource("Overworld.mp3").getPath().replace("%20", " "));
I was hoping that there was a constructor for a file object File(InputStream) so I could use the first method of loading an image (which works flawlessly so far) for a file. This, however doesn't exist.
Is there any other method of loading a file into a file object in such a way that it works in the IDE and in a compiled jar?
Thanks in advance.
(Separate but related question: why does File f = new File(this.getClass().getResource("Overworld.mp3").getFile()); give a MEDIA_UNAVAILABLE exception? The file is in the same package as "E.png" with all other source files in my project)
getResourceAsStream() returns a java.io.InputStream that you can use to read the contents of a resource in a jar as a byte array.
Will this work for you?
Why do you require a java.io.File?
Another way to load resource that placed with your class/jar.
URL url = getClass().getClassLoader().getResourceAsStream(filePath);

ClassName.class.getResourceAsStream returning Null

I migrated project from Eclipse to Android Studio.
App compiles fine, but it has a crash related to nekohtml library.
Inside HTMLEntities class
//filename = "res/HTMLlat1.properties"
final InputStream stream = HTMLEntities.class.getResourceAsStream(filename);
stream is always null. I tried to move file to the same folder as class and gave full path like this
filename = "/org/cyberneko/html/res/HTMLlat1.properties"
Any ideas?
You should use filename = "/org/cyberneko/html/res/HTMLlat1.properties" instead of filename = "/org.cyberneko.html/res/HTMLlat1.properties" or use relative paths. This could be explained in this way: jar (jar is an example, maybe you're running your code from *.class in some directory) is just some kind of file system with it's root ("/") and all the files in the packages are in some subdirectories and you should specify the paths for them.

Property file not found in JAR file

I have a project which has Main method to invoke the application. Which is bundled in a JAR file.
I am trying to invoke the application using following line:
java -jar sample.jar -sample 123
This sample.jar bundled with config/config.properties in it.
I have following line of code to read the property file from the JAR.
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("config/config.properties");
OR
InputStream inputStream =
this.getClass().getClassLoader().getResourceAsStream("/config/config.properties");
This code is not able to find property file from the JAR file, While this property file is already exist in the JAR.
Does any one know the solution on this ?
When i moved config.properties out from the config Folder, it's working fine...
I am not sure why that's the case. But if i use following line of code it's working now..
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = loader.getResourceAsStream("config.properties");

Find where Java loads files from?

I was just wondering if there is a way to find out where a java program will be searching for files.
I am trying to load a settings file with FileInputStream fstream = new FileInputStream("ldaplookup.ini"); but it is throwing a File not found error. The ini file is in the same folder as the class file but i am assuming it is searching somewhere else.
Thanks, -Pete
FileInputStream looks up the file relative to the path of execution. If the resource file is in the same folder as the class, you can try using:
InputStream stream = this.getClass().getResourceAsStream("ldaplookup.ini");
Java loads files from the current working directory for a relative path. If you want to see what is, try this:
System.out.println(System.getProperty("user.dir"));
Since "new FileInputStream("ldaplookup.ini");" is equivalent to "new FileInputStream("./ldaplookup.ini");", you could try:
System.out.println(new File(".").getAbsolutePath());
A much more reliable method to read files that are distributed with your classes is to use Class.getResourceAsStream() - it will look in the directory in the classpath where the class you're calling it on is situated, and it will even work when everything is packaged in a JAR file.
Not direct answer but a helpful alternative:
You can use a resource bundle instead.
rename ldaplookup.ini to ldaploopup.properties
And load it with:
ResourceBundle bundle = ResourceBundle.getBundle("ldaplookup");
String s = bundle.getString("url");
ResourceBundle search in the classpath for a .properties file among other strategies.
Etc. etc.
p.s.
To know what is the base path for your program try ( as suggested before: )
System.out.println(new File("."));

Categories