2

I want to access the resource's form of my project "\src\main\resources" but for any reason I can only access the target classes. Here is my code:

System.out.println(Main.class.getResourceAsStream("/123.txt")); // java.io.BufferedInputStream@66cd51c3
System.out.println(Main.class.getResource("/123.txt")); // file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt
System.out.println(Thread.currentThread().getContextClassLoader().getResource("123.txt").getPath()); // /C:/Users/Raul/workspace/Serial/target/classes/123.txt

and here my Project Dirs:

project dirs

The thing is, even if I delete all the files in the target/classes and run the code, the compiler will copy the files from "src/main/ressources" into "target/classes" and read them from there.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Raul Vasi
  • 88
  • 1
  • 11

2 Answers2

1

I want to access the resource's form of my project "\src\main\resources" but for any reason i can only access the target classes.

I think the question is answered by user @VGR. Just to clarify it in another words:

  • You put your resources in the /src/main/resources folder, and these resouces will be copied as is into the /target/classes folder when you build your project.

Example

src/main/resouces/123.txt -> target/classes/123.txt
src/main/resources/myresources/145.txt -> target/classes/myresources/145.txt
...

Now if you run the program inside of your IDE you'll observe the following:

System.out.println(Main.class.getResource("/123.txt"));

output: file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt

System.out.println(Main.class.getResource("/myresources/145.txt")); 

output: file:/C:/Users/Raul/workspace/Serial/target/classes/myresources/145.txt

But if you open the generated jar file you'll not see the target folder because the file 123.txt will be on the root of the jar file and the file 145.txt will be under the folder myresources/145.txt.

The folder target is just an output directory for the build tool and will not be packaged within your jar file.

Now to the following question:

the problem is that i dont know how to export the target classes to my jar, or how can I get "src/main/ressources" as return value.

To answer this question you have to look into your pom.xml file on the root of your project. There should be a <packaging>jar</packaging> entry in it. If that is so you create the jar file as follows:

Option 1: from the command line

mvn clean install

the jar file will be created and copied into the folder target.

Option 2: from within Eclipse (for example)

right click on the pom.xml > Run AS > Maven install

the jar file should also be generated and copied into the folder target.

Note: on your screenshot there are two jar files: core-0.0.1-SNAPSHOT.jar and Serial-0.0.1-SNAPSHOT.jar; remove them (mvn clean or right click > Run AS > Maven clean) before generating the jar file. The reason is Maven can only generate one jar file per Maven module / project, afaik.

Community
  • 1
  • 1
ujulu
  • 3,289
  • 2
  • 11
  • 14
0

You are seeing the intended behavior. A Java program is compiled into an executable form—meaning, .class files and resources. When other users run your program, they will not have access to the source, so your code should not assume your source tree will be available.

Simply put, your code is correct as is. Do not attempt to read the source tree. If you want target/classes to contain up-to-date files, rebuild your project.

A word of caution: Never use the getPath() method of URL to convert a URL to a file name. There are many characters which are not permitted in URLs, and those characters will be “percent-escaped” in order to conform to the URL specification; as a result, the path portion of a URL is not a valid filename! The only correct way to convert a URL to a file is Paths.get(url.toURI()). However, you should not even try to convert a resource to a file at all, because once you move on to packaging your programs in .jar files, your resources will not exist as regular files at all, only as entries in .jar files (which are actually just zip files with some Java-specific entries added).

VGR
  • 40,506
  • 4
  • 48
  • 63
  • but after I export my projct to a jar file and take a look in, the ressource folder is there and also all my ressourcesc. What I need is a solution to fix the ressource path's. thx – Raul Vasi Oct 10 '16 at 19:22
  • If `Main.class.getResource("/123.txt")` already returns a valid object, what’s the problem? – VGR Oct 10 '16 at 20:25
  • the problem is that i dont know how to export the target classes to my jar, or how can I get "src/main/ressources" as return value. – Raul Vasi Oct 10 '16 at 20:30
  • You cannot get "src/main/resources" as a return value. Why do you want to? Like I said, `Main.class.getResource("/123.txt")` already works. – VGR Oct 10 '16 at 20:37
  • http://stackoverflow.com/questions/27703508/read-file-from-src-main-resources I need to get that path. The runnable jar should read the images from there, or from somewhere else. pls help me. – Raul Vasi Oct 10 '16 at 21:33
  • Why can’t you use `Main.class.getResource` or `Main.class.getResourceAsStream` to read your data? – VGR Oct 10 '16 at 21:35
  • cuz, he retruns me the "target/classes" and those path will later in the jar not recognised. my jar will not find them. – Raul Vasi Oct 10 '16 at 21:42