Java FileNotFoundException when running a jar file - java

In my project I load my resource using
getClass().getResource("/package/my_reource.file").getFile()
All works good when I run the project in netbeans, but if I run the jar file, I get FileNotFoundException, why?
Thanks.

You can use InputStream rather than getClass().getResource("/package/my_reource.file").getFile()
You should use
getClass.getResourceAsStream("/package/myresource.file")

I don't think you need the filename. You rather need its content. So use getResourceAsStream() to obtain the InputStream and read the content from there.

Check your jar. I believe that your file is not there.
The reasons depend on how are you creating your jar. If you are doing it using netbeans, check your settings. Probably it includes only *.class files? The same is about ant. Check tag.

The getFile() returns the file path portion of the URL returned by getResource()
So if its in the Jar, you have to read the jar to gett he file. If its on the filesystem you can read using FileInputStream.
If you want to get the InputStream and you don't create where you get it from use getResourceAsStream()

Related

Java FileNotFoundException when trying to read txt file from resources folder

I'm trying to read a text file located in src/main/resources/test/file.txt. I'm trying to get the path of the file using String path = getClass().getResource("/text/file.txt").getFile(); but when I try to read it I get a FileNotFoundException. I tried putting many different paths, all of which failed. How can I go about doing this?
The idea of putting something into the src/main/resources tree is that it will be copied into the JAR file that you build from your project. It will then be available to your application via the Class methods getResource(String) and getResourceAsStream(String) methods.
When you are running in your application in the development environment, it is certainly possible to use FileInputStream etcetera to access the resource. But this won't work in production. In production, the resources will then be inside your app's JAR file. FileInputStream cannot open a JAR file and its contents by name.
When you do this:
getClass().getResource("/text/file.txt");
you get a URL for the resource, which will look something like this:
jar:file:/path/to/your.jar!/text/file.txt"
It is not possible to turn that into a pathname the FileInputStream will understand. Whatever you try will give you a FileNotFoundException ... or something that is not the resource you want to read.
So what to do?
You have a few options, depending on your application's requirements.
You can use getResourceAsStream and use the resulting input stream directly.
You can copy the contents of getResourceAsStream to a temporary file, and then use the pathname of the temporary file.
You can create an application specific directory (e.g. in the user's home directory) and extract the file you need from the JAR into the directory. You might do this the first time the application runs.
You could open the JAR file as a JarFile and use that API to open an InputStream for the resource. But this assumes that that the resources are in a JAR ... and on some platforms (e.g. Windows) you may encounter problems with file locking. (And it would be a bad idea to attempt to update the resource in the JAR.)
Try giving complete path of the file from the disk.
C:\Users\MyUser\Desktop\file name with extension

How to access the content of the jar during runtime?

How can I access the content of the jar file which has been started. I want to create a big jar file which contains everything I need and then during runtime I want to copy some files of my jar into an external folder. Is this possible?
You want this.getClass().getClassLoader().getResourceAsStream. Example:
InputStream config =
this.getClass().getClassLoader().getResourceAsStream("config.txt");
The files in the JAR are not accessible as files, so you must use getResourceAsStream to read them. See access files and folders in executable jars how to access the files within the jar.
Following, you use the inputstream to write the files onto the file system.
See:
Easy way to write contents of a Java InputStream to an OutputStream
http://www.mkyong.com/java/how-to-convert-inputstream-to-file-in-java/
You can access any file via the ClassPath using the classloader. Start with Class.getResourceAsStream

Referencing file from within the same JAR file as the application

I have a Java application in Eclipse that references .XML files as templates for other functionality. Usually I package the .JAR file without these files, because placing them within the same folder as the .JAR file seems to work fine with this reference:
File myFile = new File("templates/templateA.xsd");
I now require that these templates be placed within the same .JAR file as this application. I can include them with no problems, but these references no longer seem to work.
Is there a correct way of referencing the .XML file from within the same .JAR that the application is running from?
You need to know how to load the files from class path.
one of the ways is as follows
class XMLLoader {
public String loadXML(String fileName){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
// do the loading of the file from the given input stream.
}
}
you know that the "templates" folder should be inside of your jar.
If you just need to read this file, you might not need a java.io.File but just an InputStream that you can get via
this.getClass().getResourceAsStream("templates/templateA.xsd")
If you really need a java.io.File... I do not know... The last time a really needed a File, I just copied the InputStream to a temporary file but this is ugly.

Setting Path Directory on File Reader

I'm working with text files on Java. On Ubuntu 10.
But, I'm having problems with path dir.
Example:
saveFile("textFile.txt","abc");
This abstract function basically put "abc" on "textFile.txt".
I compile this file, and create a jar file (using NetBeans).
When I run the app, and call saveFile("textFile.txt","abc"), textFile.txt is saved on \home. I don't want this. I want that textFile.txtgo to pathDir inside jar file.
How do I write in this file, this same way?
When reading resources from a JAR file, you cannot use the File API. Instead, you use Class.getResourceAsStream(), like this:
reader = new InputStreamReader(MyClass.class.getResourceAsStream(
"/apathdir/textFile.txt"), "UTF-8");
Note also how the encoding is specified. FileReader does not allow that, which is why it should usually be avoided.
Iwant to know, if fileName =
"textFile.txt", what is the path dir
of this file?
If you only use a bare file name (without giving a directory), the JVM will look for the file in the current directory of the JVM process; that is usually the directory you ran the JVM (the java executable) from.
how do i do to set
/apathdir/textFile.txt?. apathdir is a
directory that is inside jar file.
I tried: fileName = "/apathdir/textFile.txt", but doesn't works.
If you want to load a file from inside a JAR file, you cannot load it using FileReader. You need to use ClassLoader.getSystemResourceAsStream() (or Class.getResourceAsStream). See e.g. this article for an explanation:
http://www.devx.com/tips/Tip/5697

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