I didn't find any tips or answers on how to stop/restart Jetty instance after running the simple HelloWorld class. Once started, no way to rerun the same class. I just took the supplied HelloWorld example and tried to re-run it after some modification. here is what I got in Eclipse console:
log4j:WARN No appenders could be found for logger (spark.route.RouteMatcherFactory).
log4j:WARN Please initialize the log4j system properly.
== Spark has ignited ...
>> Listening on 0.0.0.0:4567
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:141)
at org.eclipse.jetty.server.bio.SocketConnector.newServerSocket(SocketConnector.java:86)
at org.eclipse.jetty.server.bio.SocketConnector.open(SocketConnector.java:75)
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:356)
at org.eclipse.jetty.server.bio.SocketConnector.doStart(SocketConnector.java:146)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
at org.eclipse.jetty.server.Server.doStart(Server.java:269)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:55)
at spark.webserver.SparkServerImpl.ignite(SparkServerImpl.java:64)
at spark.Spark$1.run(Spark.java:197)
at java.lang.Thread.run(Thread.java:619)
There is no service running in Windows services list either. Any idea? Thanks
If I'm right, the problem comes from the method
public static void get(Route route) {
addRoute(HttpMethod.get.name(), route);
}
in Spark class. Then in addRoute method there is a call to 'init' method:
private static void addRoute(String httpMethod, Route route) {
init();
routeMatcher.parseValidateAddRoute(httpMethod + " '" + route.getPath() + "'", route);
}
And, finally, in 'init' method there is a check for the boolean of 'initialized' value before starting the server:
private synchronized static final void init() {
if (!initialized) {
routeMatcher = RouteMatcherFactory.get();
new Thread(new Runnable() {
@Override
public void run() {
server = SparkServerFactory.create();
server.ignite(port);
}
}).start();
initialized = true;
}
}
I think 'initialize' variable stays still 'true', that's why it happens.
how can i stop the jetty server . any chance
If you stop the java process then the jetty server will stop as well.
I believed that another more elegant solution existed, at least, for
Eclipse console(like to have a start/stop button, it is often the case for
other servers). I think, if you run the mentioned class from inside the
console/terminal, hitting CTRL-C would do it well.
Regards,
On 7 April 2013 21:52, Per Wendel [email protected] wrote:
If you stop the java process then the jetty server will stop as well.
—
Reply to this email directly or view it on GitHubhttps://github.com/perwendel/spark/issues/39#issuecomment-16022320
.
But the problem you have is strange. You don't get an address already bind if you don't keep the process running. The Jetty is not open if the process is closed, for instance in eclipse by clicking the red button in the console tab. This has got nothing to do with initialized variable. I'm not sure if I understand your problem correctly.
I would guess that if modification were made to any code one would normally close the old process before running a new one if there are components that will "keep the old process running" like servers and similar stuff.
As perwendel has mentioned, this can be avoided if red button is clicked(process stopped) before relaunching jetty.
If not, restarting eclipse will resolve this issue.
Really 'cool' solution, to restart Eclipse. If you have to restart it after
every test run, I imgine the spent time if you hav a 'real' java project in
your workspace, more often, there more then one. Really disappointed.
On 15 May 2013 15:40, HarshaKA [email protected] wrote:
As perwendel has mentioned, this can be avoided if red button is
clicked(process stopped) before relaunching jetty.If not, restarting eclipse will resolve this issue.
—
Reply to this email directly or view it on GitHubhttps://github.com/perwendel/spark/issues/39#issuecomment-17938796
.
@Javix I'm not saying that having e.g. a stop() method in the Spark class would be a bad idea but I don't understand that you're actually having this problem with, what it sounds like, spark continuing running after termination of the process. If everyone was experiencing this it would be disaster.
If you run spark from console ctrl-C Will stop spark.
If you run spark from eclipse pressing the stop-button Will stop spark. You don't have to re-start eclipse.
@Javix
My "solution" is not to restart eclipse every time, but rather to use the stop button as perwendel has mentioned. In case you forgot to stop it and re-ran the class once more, you do get this exception, to which, the easiest way to resolve would be to restart eclipse.
@Javix @HarshaKA
No you don't need to restart Eclipse. If you forget to stop the class and re-run it you will get the exception. There is nothing strange with that since the first process is occupying the port.
What you need to do is to press the double XX's on the right of the red button (see previous snapshot) to close the window with the terminated process and then click the red stop button for the first process alt. switch to the first process using the screen looking icon (which opens a drop down menu) to the far right in the snapshot and choose the running process and then close it.
Not to be rude but this is not an issue, it's lacking knowledge in eclipse and/or java.
However, as said before, a stop() method in Spark alt. access to the lifecycle could be of value but it doesn't help in this case.
Just dropping this here:
I'm enjoying Spark with Netbeans and was facing a similar issue. I was able to find and modify a bash script that worked (under Linux and OSX, not Windows support).
I've put it up in github:
For eclipse users, this might be helpful:
http://www.codeaffine.com/2016/04/19/terminate-relaunch-eclipse/
Use command for this.
Windows
get process id using
_netstat -a -o -n | findstr port_num_
kill process
_taskkill /F /PID process_id_
Ubuntu
_fuser -k port_num/tcp_
Most helpful comment
Just dropping this here:
I'm enjoying Spark with Netbeans and was facing a similar issue. I was able to find and modify a bash script that worked (under Linux and OSX, not Windows support).
I've put it up in github:
https://github.com/bliti/killnetty