0

I am working on building a web application around a Standalone JAR application. Essentially the JAR application reads an input file, processes it and generates an output file.

The input file is placed like this, right next to the JAR.

enter image description here

The code for the JAR application is shown below.

public class FileReadWriteApp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {

        File inFiles = new File("../inputFiles");
        if (!inFiles.exists()) {
            if (inFiles.mkdirs()) {
                System.out.println("Input directory is created!");
            } else {
                System.out.println("Failed to create input directory!");
            }
        }
        Path inputPath = FileSystems.getDefault().getPath("testInput.txt", new String[0]);
        Files.copy(inputPath, new FileOutputStream("../inputFiles/testInput.txt"));

        String content = new String(Files.readAllBytes(inputPath));
        String parsedInput = content.toUpperCase();

        new File("../outputFiles").mkdirs();
        Files.write(Paths.get("../outputFiles/testOutput.txt"), parsedInput.getBytes());

    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

Upon executing the JAR in the Command prompt it successfully creates inputFiles and outputFiles folders at one level higher than the JAR file and puts the corresponding input/output files correctly.

enter image description here

However, when I add the JAR as a dependency in the Spring based web application that I am building and place the input file and JAR file at the same location, it can't access the input file.

In the Spring web application I have placed both the input file and the JAR file as shown below:

enter image description here

I can even see them come up in the Target folder in the lib directory as shown below:

enter image description here

In my Spring Controller method, I am executing the main() method of the JAR file like this:

String[] args = new String[] { "" };
filereadwriteapp.FileReadWriteApp.main(args);

However, in the console I see these messages:

Input directory is created!
java.nio.file.NoSuchFileException: testInput.txt

I am not able to understand why the application can't find the file testInput.txt?

Can somebody kindly guide me regarding fixing this issue?

Is there some specific location where I should place the testInput.txt file so that the "dependency" JAR file can access it?

Kindly note that I am not loading a resource from WEB-INF directory from within the Web Application. In my case, I am having issue with a "dependency" JAR loading a resource from a relative path to itself.

Any help in this regard would be highly appreciated.

MSR
  • 173
  • 3
  • 16
  • Possible duplicate of [Howto load a resource from WEB-INF directory of a web archive](http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a-web-archive) – James Jithin Oct 06 '15 at 03:00
  • @JamesJithin, I am not sure if it's duplicate of that question. That other question is dealing with loading a resource from WEB-INF directory from within the Web Application. In my case, I am having issue with a "dependency" JAR loading a resource from a relative path to itself. – MSR Oct 06 '15 at 18:52
  • You would need to use the same approach in the dependency jar to read the file. – James Jithin Oct 07 '15 at 02:53

1 Answers1

0

FileSystems api returns different path , so you need to use classpath to get the file as shown below

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("testInput.txt").getFile());
Rai
  • 126
  • 5
  • Thanks for the pointer Rai. I had to make some edits to your code suggestion to make it compile. Basically, I had to get the ClassLoader from a static context, so I did this: `ClassLoader classLoader = FileReadWriteApp.class.getClassLoader();`. But, the second statment: `File inputFile = new File(classLoader.getResource("testInput.txt").getFile());` keeps throwing, `java.lang.NullPointerException`. I guess, the application is not able to find the file at that path. Where exactly should I place the file to make it work? – MSR Oct 06 '15 at 18:58
  • try moving it to web-inf and say /testInput.txt – Rai Oct 06 '15 at 19:05
  • I am getting confused. I have two separate projects. One is standalone Java application which is packaged as a JAR. That needs to read some files at a relative path. That project is giving me some Null Pointer Exceptions when I use the `ClassLoader` kind of syntax. There is no WEB-INF folder in that project. First of all I would need to get this standalone Java application to work fine, then I can think of putting the input file into WEB-INF folder... I think we are getting confused because of two separate applications getting integrated. – MSR Oct 06 '15 at 21:28
  • This should work for both scenarios, move the file to resources folder and try – Rai Oct 07 '15 at 02:10