1

I am trying to build a very little program with MAVEN that reads from a file but I obviosly do something wrong because it gives me FileNotFoundException.

This is how my directory structure looks like:

my-app
      src
         main|
             |
             |--java|
             |      |
             |      Main.java
             |
             |--resources|
                         |
                         res.txt

The res.txt contains some Strings that I'd like to read from Main.java

...
File file = new File("res.txt");
FileReader fr= new FileReader(file);
BufferedReader br = new BufferedReader( fr);
...

I compile it with : C:\my-app mvn package

It creates the jar under target. When I peep inside the jar, I see it contains both Main.class and res.text next to each other.(under the same directory)

Still, when I try to run it :
java -cp target/my-app-1.0-SNAPSHOT.jar Main

I gives my FileNotFoundException, Main.class somehow does not see res.txt file. Why? Where should I put res.txt? What else should I configure?

Any help would be greatly appriciated.

Sanyifejű
  • 2,610
  • 10
  • 46
  • 73

3 Answers3

0

I assume the .txt file is copied to the target folder and part of the jar file?

In that case use a resource loader to retrieve it from the classpath and not from the file-system.

see read file in classpath for some examples how to load files.

Community
  • 1
  • 1
wemu
  • 7,952
  • 4
  • 30
  • 59
  • when I peep into the jar I see this: ----META-INF(directory) ----Main.class ----res.text – Sanyifejű May 10 '12 at 13:19
  • thats perfect. You should be able to read the file from the classpath. – wemu May 10 '12 at 13:21
  • like this? BufferedReader br= new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("res.txt"))); – Sanyifejű May 10 '12 at 13:39
  • Sadly thats the result: Exception in thread "main" java.lang.NoClassDefFoundError: App Caused by: java.lang.ClassNotFoundException: App at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) – Sanyifejű May 10 '12 at 13:57
  • anyway as I had more chance to play with it I found that the question is more classpath related than maven related. If I unpack the jar and run the class from the aprropriate place than it DOES find the res.text But if I try to run it outside of the jar like this: java -cp target/my-app-1.0-SNAPSHOT.jar Main than it DOES NOT find the textfile. So the problem must be classpath related I beleive – Sanyifejű May 11 '12 at 07:38
0

You may try with

File file = new File(this.getClass().getResource("/res.txt").getFile());

or even

File file = new File(this.getClass().getResource("/res.txt").toURI());
magnum87
  • 324
  • 1
  • 11
  • anyway as I had more chance to play with it I found that the question is more classpath related than maven related. If I unpack the jar and run the class from the aprropriate place than it DOES find the res.text But if I try to run it outside of the jar like this: java -cp target/my-app-1.0-SNAPSHOT.jar Main than it DOES NOT find the textfile. So the problem must be classpath related I beleive – Sanyifejű May 11 '12 at 07:35
0

InputStream TokenStream = this.getClass().getResourceAsStream("/files/Token.xlsx");

XSSFWorkbook wb = new XSSFWorkbook(TokenStream);

XSSFSheet sheet = wb.getSheetAt(0);

This works for reading excel file

Mr. Kulak
  • 21
  • 3