0

I have a following issue. My project structure is that of a typical maven Java project:

src/main/java - project .java files      
src/main/resources - project resources (log4j2.xml)
src/test/java - .java files for tests
src/test/resources - testng.xml file

In the src/main/java I also have a package with JavaScript files (some scripts that I'm executing programmatically in Java code) at the following location: src/main/java/js/scripts/

Naturally I can access the *.js files inside the 'scripts' folder when I'm running tests - I'm simply using FileUtils.readFileToString(new File(pathTo_jsFile))

However after compilation of the project with maven this is no longer working.

  1. I'm unable to compile with the files to be placed inside the existing package (/js/scripts/) - the only way I was able to add non-java files to the .jar is to add them as resources in pom.xml, but then they are getting added to the root of .jar file.

  2. I'm unable to read anything from within the .jar file.

SergioLeone
  • 734
  • 1
  • 10
  • 24
  • To read a .jar, see [How to add and read resource file from jar](http://stackoverflow.com/questions/5466685/how-to-add-and-read-resource-file-from-jar?rq=1). – KevinO May 16 '17 at 07:15
  • I'm sorry, I should have mentioned the things I've already tried. I have tested the getResourceAsStream() and it gives a null. I've read that this method can only read from the project class file, not from compiled .jar – SergioLeone May 16 '17 at 07:28
  • 1
    You read wrong... – Steve C May 16 '17 at 07:32
  • Yes, it can return a null if the resource isn't found, but it most certainly does read from a .jar. Most likely, the path to the resource is specified incorrectly. It is also possible the resources aren't in the .jar. – KevinO May 16 '17 at 07:33
  • @Sergio: did you try `Class.getResourceAsStream()` or `ClassLoader.getResourceAsStream()`? The differ a little bit in behaviour, if you need a leading / (Class) or not (ClassLoader) to specify an absolute path within the JAR. – cyberbrain May 16 '17 at 07:34
  • I have finally managed it to work and it was a stupid mistake of mine... All I needed to do is convert the stream to string and it worked. Thanks all – SergioLeone May 16 '17 at 07:54

1 Answers1

0

First you should put your JavaScript files into the src/main/resources folder. There they should be packed into the .jar file by maven without any special handling by maven. You can keep your folder structure in the resources folder.

Second you can access resources in Java with the help of ClassLoader:

getClass().getClassLoader().getResourceAsStream("js/scripts/myScriptfile.js");

This should get you the javascript file as input stream. If you prefer an URL to that file (yes, works also when it's contained within a JAR file), just use getResource(...) instead of getResourceAsStream(...).

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • I have tried `getClass().getClassLoader().getResourceAsStream` but it does not read from the .jar. – SergioLeone May 16 '17 at 07:25
  • 1
    Did you open the .jar file (with any tool that can read ZIP-files) and check, that the files are inside? Next: you have to specify the correct (relative) path inside the JAR-file! – cyberbrain May 16 '17 at 07:27
  • Yes, I can see that the files are inside, the path is also correct (there was also a '!' sign when trying to read the path that points to a jar and I've also tried removing it from the string), but it still gives null pointer. – SergioLeone May 16 '17 at 07:32
  • Just to cross check: does the `.getResourceAsStream()` method work, when your code is not put into the JAR but runs "exploded" on disk? And just FYI: the ! in the path is correct for file paths within a JAR. Don't remove it. – cyberbrain May 16 '17 at 07:38
  • 1
    `getClass().getClassLoader().getResourceAsStream` is the standard way to accomplish what you're looking for; however, sometimes you need to be carefull which classloader you use (depending on your setup). So perhaps try with `Thread.currentThread().getContextClassLoader()` ? – Andrei May 16 '17 at 09:05