Okay, so the issue I’m being stuck at for few days already is that Tomcat doesn’t run my applications. This is my first encounter with Tomcat and server programming overall, so its most likely purely my fault. But in few days I couldn’t beat it myself, so I decided to post a question here.
I have tried running quite a lot of different setups, but now I am at the easiest one and still can’t figure it out. So, I’ve got installed: Eclipse IDE 1.5.0 (Eclipse platform 4.2.0), WST 3.4.1, Tomcat 7.0.12 installed through Eclipse (tried 7.0.30 also).
I’ve got tomcat server configured in eclipse fine, so when I run it – it does work as intended.
But then I’ve got this easy piece of code which I’ve got from oracle’s guide on server sockets:
import java.net.*;
import java.io.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class KnockKnockServer {
private static final Logger logger = Logger.getRootLogger();
public static void main(String[] args) throws IOException {
BasicConfigurator.configure();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
logger.debug("Socket opened");
System.err.println("Port opened");
} catch (IOException e) {
logger.debug("Could not listen on port: 4444.");
System.err.println("Port not opened");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
logger.info("socket accepted");
} catch (IOException e) {
logger.info("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
which doesn’t work when I do the “Run As -> Run on server” launch. I tried adding it to WAR and deploying, but it yields the same result – none. Meaning that niether I can connect to it using another client app (from another eclipse instance) nor it produces ANY messages showing it works (only accesss logs produced by tomcat). As you can see I added log4j to the code to see any message that proves the code actually RUNS on server (initially there were just System.err’s and System.out’s), which I tried configuring both for console output and file output.
And again, the server itself runs as intended – I can access it through browser, open manager app, deploy/undeploy apps, etc.
So the question is: what am I doing wrong? I assume there might be some primitive mistake/undone step/whatsoever made by me, but I couldn’t figure it out in 2 days of fighting with help of google.
Help will be greatly appreciated.
UPDATE: Thanks a lot guys! As I suspected – stupid me 🙂 Don’t know why I started this mess with Tomcat, but yeah, Run as Application works fine. Those instant answers saved me a lot of time. Thanks a lot again.
I think there is a misunderstaning here. Tomcat is a servlet container – it’s intended to run web applications that consist of servlets.
Your
KnockKnockServeris a regular Java application, therefore running it “on Tomcat” makes absolutely no sense. You should run it as a standalone application instead.Actually, sometimes it makes sense to start a server for some specific protocol inside a web application that runs on Tomcat. In this case you need to control lifecycle of the custom server from
ServletContextListener. But I don’t think that it’s your case.