OiO.lk Blog java Running a Java application deployed as WAR on Tomcat server
java

Running a Java application deployed as WAR on Tomcat server


I have a very simple Java application, all it does is write a file with the current date as the title and, a single line in the file which is also the current date. I just made this as a test:

package webfilewriter;

import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *
 * @author bhemrik
 */
public class WebFileWriter {

    public static final String FILE_PATH;
    
    static {
        FILE_PATH = "resources\\";
    }

    public static void main(String[] args) throws IOException {
        LocalDateTime now = LocalDateTime.now();
        String nowString = timestampToString(now);
        writeToFile(nowString);
    }

    private static String timestampToString(LocalDateTime now) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
                "yyyy-M-d__HH-mm-ss");
        return now.format(formatter);
    }

    private static void writeToFile(String nowString) throws IOException {
        FileWriter writer = new FileWriter(FILE_PATH + nowString);
        writer.append(nowString);
        writer.close();
    }
    
}

I have built this simple application as a WAR file, and deployed it on my Tomcat server. N

ow obviously this has no user interface to it. No Servlets HTML pages or anything of that sort, the entry point should be the main() method, regardless the fact that it is deployed on a tomcat server.

The question is how do I tell Tomcat to run it? I don’t want to implement a context listener or anything really, I dont even need it to be runable from a browser, I just want to be able to run it over the internet somehow, no ssh-ing into the server.

Isn’t tomcat supposed to be providing me with this functionality? What am I missing? How can I make this work?



You need to sign in to view this answers

Exit mobile version