2

I have a Java Spring application (maven) with a TranslationService class, which returns translated string from a JSON file (backend.json) .. When I build my project (mvn clean package) I get my {project}-{version}.jar file. If I execute this file (java -jar target/*.jar), all works perfectly, I can see my page, and everything works.

But I have one problem now. In the production version of my project I use Apache2 with VirtualHost. My test config looks like:

<VirtualHost *:80>
        ServerName myloc
        ProxyPass / http://localhost:8080/
</VirtualHost>

I can still see my page and fill my forms etc.. But if I after my final submit, my application sends me an email and creates a pdf for me. But my application crashes, because it can not translate my stuff, because it says, it cant find my file. But in my default (tomcat) version it worked fine.

static String getTranslation(String lang, String jsonKey) {
    try {
        File file = new File("./src/main/resources/static/i18n/"+lang+"/backend.json");
        JsonReader reader = Json.createReader(FileUtils.openInputStream(file));
        // ...

error:

"message":"java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.io.FileNotFoundException: File './src/main/webapp/i18n/de/backend.json' does not exist"
  • A path is relative to the working directory, simply print the absolute path with `System.out.println(file.getCanonicalPath())` to know why your resulting path is incorrect – Nicolas Filotto Nov 21 '16 at 13:41

2 Answers2

1

Since your path is relative to the working-directory, you should at first check, if you placed the file at the right location.

To log the working directory you could get it like this:

   System.out.println("Current dir:"+new java.io.File( "." ).getCanonicalPath());

Or you can use an absolute path to your file (which I would nor recommend).

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
1

Right now you check the path relative from the programs working directory!

But your file is located in the resources folder and packed into the jar so you won't find "./src.... "

You should access resources in a different way. (example: click me)

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Nordiii
  • 446
  • 1
  • 3
  • 10