How to Start Selenium Server with Java Code

When test automating a web application using Selenium, we have to start the Selenium server first, so that a new Selenium session is created to talk to the web browser. This can be either done manually, i.e. user running a command line to start the Selenium server, or to get the pure automation effect of Selenium, it is best to start the Selenium server via a program code. The code below is written in Java and starts the Selenium server when called. Normally this would be the first action within your main() function.

Another very simple and easy way to start selenium server inside the program, and if you are not bothered about setting specific port, just call these few lines in your program (before starting selenium session):