Load a file outside of a Jar - java

I have a folder called lib that contains all my Jar files and in one of the Jar files class, I have a main method which is called by a batch file. In the same folder location as my lib, I have another folder structure path/to/a/resource/myresource.txt
How can I load this file from a class inside the Jar file? I tried the following and both resulted in null:
getClass().getResource("path/to/a/resource/myresource.txt")
getClass().getClassLoader().getResource("path/to/a/resource/myresource.txt")
Any ideas? Even with an absolute path, it failed! Any suggestions?

You can use:
getClass().getResourceAsStream("path/to/a/resource/myresource.txt")
However, for this to work, you need to add the path '.' to the Class-Path entry of the JAR's MANIFEST.MF file.
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Two things you tried are used to read files from class-path since this folder is not on your classpath you can read it directly with any of the java File IO classes.

File file = new File("C:/folder/myFile.txt");
or if you know the relative path:
File file = new File("../../path/myFile.txt");

Your path seems not to be precise enough. Further, this question has been worked before.
Have a look here:
How to Load File Outside of, but Relative to, the JAR?
How to get the path of a running JAR file?

You can either load the file from file system
new FileReader(relativeOrAbsoluteFilesystemLocation)
or you can add the directory in question to your classpath:
java -cp "lib/*;lib" ...
and then use your original method.
(Unix uses : rather than ; as classpath separator)

Related

Java can't read file except when in project root directory

I am using IDEA 14 to follow a simple Java tutorial (JDBC). As part of this, I am storing some configuration properties in a file called jdbcTutorial.properties. When I put this in the root directory of the project, I can read it with the following:
Properties props = new Properties();
props.load(new FileInputStream("jdbcTutorial.properties"));
However, as soon as I move it to any other directory in the project, I get the error "No such file or directory". This happens regardless of whether I specify a relative or absolute path:
Maybe there are more standard ways to use config files, but I would really like to understand the behavior I am observing. Thanks for helping!
By default root directory would be added to your project's build path. Since the directory in which you are putting the file is not added in your project's build path jvm is unable to find the file. You have two options:
Add the folder where you are putting your prop to build path.
Access the file with full path i.e. /home/user/workspace/....
When you build a project, IDEA takes the files in the resources directory and puts them in the executable jar. So to get an input stream from that file, you need to get it directly from inside the jar. Instead of FileInputStream, use
getClass().getResourceAsStream("jdbcTutorial.properties")
When no path is supplied for a file, java assumes that this file is in the project's root folder. So the relative path "." always points to this folder. When the file is somewhere else, put the appropriate relative path before the path name, like "./files/configuration/jdbcTutorial.properties".
It work also when you put an absolute file path before the path name, like "C:/Users/Me/Documents/Java/workspace/thisProject/files/configuration/jdbcTutorial.properties".

Path to file in src directory

I am using a method that requires a string path to a file that is in my src directory structure in eclipse. Is the path to this file simply src\fileName.txt or is there a different way i should be getting this file as it doesnt seem to be working currently
Thanks
Run this and you will never forget how to remember.
File file = new File("sample.txt");
System.out.println(file.getAbsolutePath());
If you are following the maven standard directory structure, class files will be located relative to the classpath like so:
If you want SomeClass.class, you would access it by com.mydomain.packageorappname.deeperpackage.SomeClass. Is that what you are asking?

Getting XML file not in the same root as the source file in Java

I have a problem where I can't seem to link to a xml file, see the layout below:
Folder Name
-Folder
-Folder
-SourceFiles
-packagename
-all my java files
-myXml.xml
Build is where all the class files etc is stored.
src is where the projectFolder is, and within it the java files
Code I am using to link XML File for Synth: SynthDialog.class.getResourceAsStream("synthtest/synthDemo.xml")
Now I want to link to the myXML.xml file in the top-level folder. It would be the PHP Equivelent of ../../Folder/
Thanks
You appear to be attempting to access the file using getResourceAsStream with a relative name. If that is the case, then the resource should be in located in a JAR file or directory on the classpath, and the location will be resolved relative to the FQN of the class.
I can't tell where the ".class" files are located in the tree, or how your classpath is set up, so I can't be more specific.
UPDATED
If you are executing out of that build directory, then your build process needs to copy the XML file to the appropriate place in the build tree so that the class-relative path ends up referring to the file. (Or use a path that starts with "/" so that you don't depend on the classes FQN at all.)
In the long term, you will probably execute out of a JAR file, and the data file will need to be inside it.
Use "getSystemResourceAsStream" instead of "getResourceAsStream" to access files outside of your codebase.

FileNotFoundException in Netbeans

I have a java application project in Netbeans. I have just one class.
I try to do this
FileReader fr = new FileReader("sal.html");
I have the file sal.html under the same package. But I get this error when I run:
Errorjava.io.FileNotFoundException: sal.html (The system cannot find the file specified)
My guess is that Netbeans is invoking the JVM from your project's root folder. Quoting a portion of the File Javadoc:
By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
To verify relative path resolution you could try:
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
You could then move your file to wherever java is looking for it. Most probably your project's root folder.
You could also consider using the class loader to read files as resources inside packages using getClass().getResourceAsStream("sal.html");. This is the preferred way of accessing resources since you no longer have to worry about absolute vs. relative paths. If a resource is in your classpath, you can access it. See this answer for more.
Put your file to main project folder. Not to any sub folders like src, or bin etc. Then it will detect your file.
Click on file view in Netbeans. Move sal.html to the project folder. Such that you will see it like this
- JavaProject
+ build
+ lib
+ nbproject
+ src
+ build.xml
manifest.mf
sal.html
Now
FileReader fr = new FileReader("sal.html");
will work.
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
Then it will show where the JVM is retrieving the files from. Usually for linux in the /home/username/NetbeansProjects/ApplicationName/.
Put your resources or files to this path
I think your problem is in the relative path to the file. Try to declare FileReader with full path to file.
FileNotFoundException means file not found.
The build folder for the netbeans is different where there is no file sal.html.
Try using absolute path in place of using relative path.
This is not a "File not found" problem.
This is because each class hold its own resources (let it be file, image etc.) which can be accessed only through a resource loader statement which is as below:
InputStream in = this.getClass().getResourceAsStream("sal.html");
The only fix is that you will get an InputStream instead of a file.
Hope this helps.

How to specify relative file path in Java file so that it can still work after the file is put in jar file?

Suppose I have a Java class that needs to access a file with absolute path
/home/gem/projects/bar/resources/test.csv:
package com.example
class Foo {
String filePath = ????? // path to test.csv
String lines = FileInputStream(new File(filePath).readAllLines();
}
Where the path to Foo.java is /home/gem/projects/bar/src/com/example.
Of course I cannot specify absolute path to the resource file. This is because jar file will be distributed as library for any clients to use in their own environments.
Assume the resource file like test.csv is always in the same path relative to project root. When a jar is created containing Foo.class, this jar also contains test.csv in the same relative path ( relative to project root).
What is the way to specify relative path that would work no matter where the project bar is moved to? Also how can I create a jar file (which can be in any location) so that the path to the resource file test.csv would still be correct.
To keep things simple, I have used invalid Java API ( readAllLines() which reads all the lines and return a string containing entire file content. Also not using try/catch).
Assume csv file can be read as well as written to.
I hope this makes it clear now.
Put the test.csv file into the src folder and use this:
Foo.class.getResourceAsStream("/test.csv")
To get an InputStream for the file. This will work wherever the project is moved, including packaged as a JAR file.
Example:
ProjectX\src\Test.java
ProjectX\resources\config.properties
If you have the above structure and you want to use your config.properties file, this is how you do it:
InputStream input = new FileInputStream("./resources/config.projects");
In this example you don't have to worry about packaging your source into jar file. You can still modify your resources folder anytime.
Use getResource(), as shown here.

Categories

Resources