2
votes

I have a txt-file in my web-app folder which I want to read. It contains a lot of text which should be stored in some database objects during startup so instead of putting the text into bootstrap.groovy, I wrote a reader method in a separate Util-Class. The util-class is placed in Grails utils folder and is injected into bootstrap.groovy. It works perfectly on my local machine when running "grails run-app" but the deployed war on my server just says

2014-01-15 17:38:23,508 [localhost-startStop-1] ERROR context.GrailsContextLoader - Error initializing the application: null java.lang.IllegalArgumentException at FileUtils.readFile(FileUtils.groovy:23) at BootStrap$_closure1.doCall(BootStrap.groovy:260) at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308) at grails.util.Environment.executeForEnvironment(Environment.java:301) at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) at java.util.concurrent.FutureTask.run(FutureTask.java:166) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:724)

It means this piece of code:

        println ("PATH: " + org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getRealPath('hilfethemen.txt'))

Any suggestions? I've tried a lot but many blog posts are outdated. Thank you!

EDIT: My Bootstrap code looks like this:

class BootStrap {

    def statladsService

    def init = { servletContext ->
 // read helptopics

        ArrayList<String> hilfethemen = new ArrayList<String>();


        String content = FileUtils.readFile("hilfethemen.txt")
        println ("STRING: " + content)
        if(content!=null) {
            for(String hilfethema : content.split("<hilfethema>")) {
                hilfethemen.add(hilfethema);
            }

            ArrayList<String> clearLines = new ArrayList<String>();

            for(String hilfethema : hilfethemen) {

                def page = new Page()

                for(String line : hilfethema.split("\n")) {
                    if(line.split("=")[0].equals("title")) {
                        // title
                        page.title=line.split("=")[1];
                    } else if(line.split("=")[0].equals("content")) {
                        // content
                        String lineContent=line.split("=")[1];
                        page.content=lineContent
                    }
                }
                page.save(flush:true)
            }
        }
     }
}

Util-Class actually looks like this:

import org.apache.commons.io.IOUtils
import sun.nio.cs.StandardCharsets

import java.io.*;

public class FileUtils {

    /**
     * Reads a file and returns its content as UTF-8
     * @param path
     * @return
     */
    public static String readFile(String path) {

        String content = "";
        /*
        File fileDir = new File(path);
        if(!fileDir.exists()) {
            return null;
        }
        */

        println ("PATH: " + org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getRealPath('hilfethemen.txt'))
         BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getRealPath('hilfethemen.txt')), "UTF-8"));



        String str;

        try {
            while ((str = buf.readLine()) != null) {
                content+=str+"\n";
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        try {
            buf.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return content;
    }
}
1
Can you paste the code in the bootstrap.groovyHubert
Just did @hubertboatenguser3177614
did you check if you have the same java version on the server you deploy your application on?Hani
can you drop the text file in src/java?Karthick AK
i guess there is no file 'hilfethemen.txt' in server , try using runwar in grails and make sure the file in added in war.Sachin Verma

1 Answers

0
votes

Got it. Even when I used this kind of code in a controller it gave me the error. I decided to check which Tomcat version I am running. Sadly it was Tomcat 8 which seams to have some bugs like (http://mail-archives.apache.org/mod_mbox/tomcat-users/201308.mbox/%[email protected]%3E) wich should be fixed in RC10 but did not work with Grails anyway. I just downgraded to Tomcat 7 and it works now... I just should keep my eyes open and do not install an alpha next time. Thank you guys.