0

I am trying to run a script on my tomcat webserver. To run the script before on my local machine, this is the code I used.

String absolutePath = new File(".").getAbsolutePath();
        int last = absolutePath.length()-1;
        absolutePath = absolutePath.substring(0, last);
        String filePath =  "";
        if(osVersion.equalsIgnoreCase("Ubuntu"))
        {
            try (FileReader fr = new FileReader("template.txt");
                    FileWriter fw = new FileWriter("Ubuntu/ubuntu_file.json");) {
                   int c = fr.read();
                   while(c!=-1) {
                       fw.write(c);
                       c = fr.read();
                   }
               } catch(IOException e) {
                   e.printStackTrace();
               }
            filePath = "Ubuntu";
            String fi = absolutePath + filePath;
            System.out.println(fi);//Get the full path.
            // Create ProcessBuilder.
            ProcessBuilder pb = new ProcessBuilder("bash", "-c",
                    "cd "+fi+" ; PACKER_LOG=1 /usr/local/bin/packer  build ubuntu_file.json");

            Process p = pb.start();

When I however try to run it on the tomcat webserver, I keep getting this error.

EclipseEE.app/Contents/MacOS/Ubuntu Failed to parse template: open ubuntu_file.json: no such file or directory

I am fairly new to Tomcat, and I am just learning it's ins and outs. What tomcat directory should I place my Ubuntu folder (I am assuming it's the webapp directory) in order for tomcat to get the absolute path of the folder and then be able to run the script.

user3078335
  • 781
  • 4
  • 13
  • 24
  • 2
    You should never depend on the current directory of a web server (e.g. Tomcat). Always use fully qualified file names in web application code. – Andreas Mar 29 '17 at 22:48
  • I am sorry if this sounds dumb. I am new to all of this. How should I go about doing it then? It originally was not supposed to be a web app. – user3078335 Mar 29 '17 at 22:51
  • If you don't want to hardcode a qualified file name (not a good idea), it means your webapp needs to the ability to be *configured*, e.g. using [servlet initialization parameters](http://stackoverflow.com/q/14665037/5221149). – Andreas Mar 29 '17 at 22:55

1 Answers1

0

If you have a more or less conventional Tomcat installation then the $CATALINA_HOME environment variable will be set and point to your server installation which will contain at least the following directories:

$CATALINA_HOME/
   bin/
   conf/
   lib/
   webapps/

You can get the value of $CATALINA_HOME via:

String catalinaHomeDir = System.getenv("CATALINA_HOME");

I would be inclined to put your configuration in the conf subdirectory.

If you're running multiple Tomcat instances from the same base then be sure to read the RUNNING.txt file that comes with it because you may need to use $CATALINA_BASE instead.

You may need to set up CATALINA_HOME/BASE in your Eclipse Tomcat Runtime environment when running locally with an Eclipse controlled server.

BTW. This is not a portable solution. If you need to migrate to some other container (such as WildFly or Glassfish) then the absolute path config recommended by others is the way to go.

Steve C
  • 18,876
  • 5
  • 34
  • 37