我想讓一個Java應用程序監聽一個端口。我從命令提示符運行所需的命令,它監聽端口,我看到Server is listening on port 7070
作爲輸出,命令行不能通過低位線。如果關閉命令提示符,則會斷開連接,然後關閉該端口。不斷收聽端口
下面是相關代碼:
import java.io.InputStream;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import org.xml.sax.InputSource;
import de.l3s.boilerpipe.document.TextDocument;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import de.l3s.boilerpipe.sax.BoilerpipeSAXInput;
// Language detect librarys
import com.cybozu.labs.langdetect.*;
import net.arnx.jsonic.JSON;
import net.arnx.jsonic.JSONException;
import java.io.*;
import java.net.*;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
class ExampleProgram {
public static void main(String[] args) throws Exception {
EveryDetector evr = new EveryDetector();
InetSocketAddress addr = new InetSocketAddress("localhost",7070);
HttpServer server = HttpServer.create(addr, 0);
MyHandler hndl = new MyHandler();
hndl.setDetector(evr);
MyHandlerExtractContent hnd2 = new MyHandlerExtractContent();
hnd2.setDetector(evr);
MyHandlerDetectLanguage hnd3 = new MyHandlerDetectLanguage();
hnd3.setDetector(evr);
server.createContext("/",hndl);
server.createContext("/extractcontent",hnd2);
server.createContext("/detectlanguage",hnd3);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port 7070");
}
}
我該怎麼辦?
你想關閉你的shell /窗口後繼續java程序嗎?請編輯澄清。 –
關閉shell後,Java程序不會繼續。這是主要問題。 – Stewie
如果你在linux下運行,你可以使用'nohup'命令。請參閱[這裏](http://stackoverflow.com/questions/4797050/how-to-run-process-as-background-and-never-die) –