Christian Peters Christian Peters - 18 days ago 5
Java Question

Java can't find my translation file if I use Apache2 (instead of Tomcat)

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"

Answer

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)