How do I create a resource so that it is located in the resource folder of my project?
In the following, "test.txt" is a file I want to create, but the variable:url is null, so I can't get a path to the file I want to create.
URL url= HashArray.class.getResource("test.txt");
File file = new File(url.toURI());
The resource probably needs to be located in the resources folder because I need to bundle it with the code in the packaging phase.
This can't work, as resources is not a runtime location. It's a source location. If you try to put a file, and run your program the url wouldn't be null anymore. But it would point to your target/classes folder (ok there are quite a number of possiblities, depending how exactly you start it) However, in most cases it wouldn't be resources anymore...
There is not relation between the resource folder in maven and a resource in Java.
class.getResource finds a resource in the classpath, you are fine putting your txt file in the resources folder but in order to make it accessible at runtime you have to package it.
How to package resources
Related
I am trying to get a properties file from /src/main/resources/properties/ but for some reason the following code returns the path of target classes instead of src files. Can you please help?
System.out.println(PropTest.class.getResource("/properties/app.properties"));
System.out.println(PropTest.class.getClassLoader().getResource("properties/app.properties"));
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
System.out.println(classloader.getResource("properties/app.properties"));
PropTest prop=new PropTest();
System.out.println(prop.getClass().getResource("/properties/app.properties"));
Each line gives the same output which is:
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
file:/C:/Users/b./eclipse-workspace/ordermonitoring/target/classes/properties/app.properties
The behavior is correct. The resources will be loaded from class path. There wont be any src folder at runtime. What you can do is make the /src/main/resources as a source folder (if you are not using maven) so that the properties will be copied to target folder.
This is the expected behaviour. For maven projects, while running, project is build and is kept in target folder always. you can also change this folder by some configuration. From this target folder, your application runs and thus your path shown in classloader.getResource() method. If you are building a jar , then your resources will be inside the jar and is always available. So classloader.getResource() will be working fine always.
So, I've got some txt files that I want to place in the following folder: src/test/resources/__files
However, when I run my project, I find that the compiler is looking for files in the project folder. Is there anyway of changing this as it becomes problematic when I try to create a JAR file - those files are then needed to be manually added to the target folder.
Thanks
Since you are already placing your resource files in the src/test/resources folder, Maven should make them available on the classpath at runtime. In this case, they should be on the classpath in your test build. You may try the following code:
String fileName = "somefile.txt";
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
The Mkyong site does a good job of explaining how resources work in Maven.
You seem to be working with the maven build tool, with per convention src/main/java, src/main/resources and for unit tests src/test/java and src/test/resources.
And then the files are not real file system File, but resources, packaged possibly in a jar (as you said): case-sensitive, / path separator and read-only InputStream.
You could keep the file as resource (under /src/main/resources/ then), and use that file as template for a real File, at the application System.getProperty("user.dir") (working directory), or System.getProperty("user.home") (home directory), see System Properties.
InputStream in = .... .class.getResourceAsStream("___files/xyz.txt");
Or you could in maven copy from your own location to the target/distribution directory.
I use NetBeans on Windows I write String url = "src\\test\\resources\\__files"
Folder structure
ProjFolder\src\test\resources\__files
I'm trying to load a .csv file in a program but for some reason, it's unable to find the file. Where should I place the file?
Console
It looks like the file is in the src directory... which almost certainly isn't the working directory you're running in.
Options:
Specify an absolute filename
Copy the file to your working directory
Change the working directory to src
Specify a relative filename, having worked out where the working directory is
Include it as a resource instead, and load it using Class.getResourceAsStream
The file is located in the src directory so in order to access it you should use
src/Elevator.csv
As long as files are located inside your project folder you can access them using relative paths.
For example if a file is located under the Elevator folder then you access the file by using only its filename.
Elevator.csv
A good principle when using additional files in your project is creating separate folders from the ones that the source files are located. So you could create a folder resources under the project folder and place your file there. You can access then the file by using
resources/Elevator.csv
the path which it is trying to read is surely not exact as the path in which that file is actually present.Try printing absolute path of that file and compare it with actual path of your file.
I tried with all the above mention solution, but it didn't work..
but i went to my project folder and delete the target and tried to compile the project again. it then worked successfully
Currently, in my eclipse project, I have a file that I write to. However, I have exported my project to a JAR file and writing to that directory no longer works. I know I need to treat this file as a classpath resource, but how do I do this with a BufferedWriter?
You shouldn't have to treat it as a classpath resource to write to a file. You would only have to do that if the file was in your JAR file, but you don't want to write to a file contained within your JAR file do you?
You should still be able to create and write to a file but it will probably be relative to the working directory - the directory you execute your JAR file from (unless you use an absolute path). In eclipse, configure the working directory from within the run configuration dialog.
You're probably working in Linux. Because, in Linux, when you start your application from a JAR, the working directory is set to your home folder (/home/yourname/). When you start it from Eclipse, the working directory is set to the project folder.
To make sure you really know the files you are using are located in the project folder, or the folder where your JAR is in, you can use this piece of code to know where the JAR is located, then use the File(File parent, String name) constructor to create your files:
// Find out where the JAR is:
String path = YourClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
path = path.substring(0, path.lastIndexOf('/')+1);
// Create the project-folder-file:
File root = new File(path);
And, from now on, you can create all your File's like this:
File myFile = new File(root, "config.xml");
Of course, root has to be in your scope.
Such resources (when altered) are best stored in a sub-directory of user.home. It is a reproducible path that the user should have write access to. You might use the package name of the main class as a basis for the sub-directory. E.G.
our.com.Main -> ${user.home}/our/com/
Lots of confusion in this topic. Several Questions have been asked. Things still seem unclear.
ClassLoader, Absolute File Paths etc etc
Suppose I have a project directory structure as,
MyProject--
--dist
--lib
--src
--test
I have a resource say "txtfile.txt" in "lib/txt" directory. I want to access it in a system independent way. I need the absolute path of the project.
So I can code the path as abspath+"/lib/Dictionary/txtfile.txt"
Suppose I do this
java.io.File file = new java.io.File(""); //Dummy file
String abspath=file.getAbsolutePath();
I get the current working directory which is not necessarily project root.
Suppose I execute the final 'prj.jar' from the 'dist' folder which also contains "lib/txt/txtfile.txt" directory structure and resource,It should work here too. I should absolute path of dist folder.
Hope the problem is clear.
You should really be using getResource() or getResourceAsStream() using your class loader for this sort of thing. In particular, these methods use your ClassLoader to determine the search context for resources within your project.
Specify something like getClass().getResource("lib/txtfile.txt") in order to pick up the text file.
To clarify: instead of thinking about how to get the path of the resource you ought to be thinking about getting the resource -- in this case a file in a directory somewhere (possibly inside your JAR). It's not necessary to know some absolute path in this case, only some URL to get at the file, and the ClassLoader will return this URL for you. If you want to open a stream to the file you can do this directly without messing around with a URL using getResourceAsStream.
The resources you're trying to access through the ClassLoader need to be on the Class-Path (configured in the Manifest of your JAR file). This is critical! The ClassLoader uses the Class-Path to find the resources, so if you don't provide enough context in the Class-Path it won't be able to find anything. If you add . the ClassLoader should resolve anything inside or outside of the JAR depending on how you refer to the resource, though you can certainly be more specific.
Referring to the resource prefixed with a . will cause the ClassLoader to also look for files outside of the JAR, while not prefixing the resource path with a period will direct the ClassLoader to look only inside the JAR file.
That means if you have some file inside the JAR in a directory lib with name foo.txt and you want to get the resource then you'd run getResource("lib/foo.txt");
If the same resource were outside the JAR you'd run getResource("./lib/foo.txt");
First, make sure the lib directory is in your classpath. You can do this by adding the command line parameter in your startup script:
$JAVA_HOME/bin/java -classpath .:lib com.example.MyMainClass
save this as MyProject/start.sh or any os dependent script.
Then you can access the textfile.txt (as rightly mentioned by Mark) as:
// if you want this as a File
URL res = getClass().getClassLoader().getResource("text/textfile.txt");
File f = new File(res.getFile());
// As InputStream
InputStream in = getClass().getClassLoader()
.getResourceAsStream("text/textfile.txt");
#Mark is correct. That is by far the simplest and most robust approach.
However, if you really have to have a File, then your best bet is to try the following:
turn the contents of the System property "java.class.path" into a list of pathnames,
identify the JAR pathname in the list based on its filename,
figure out what "../.." is relative to the JAR pathname to give you the "project" directory, and
build your target path relative to the project directory.
Another alternative is to embed the project directory name in a wrapper script and set it as a system property using a -D option. It is also possible to have a wrapper script figure out its own absolute pathname; e.g. using whence.