简体   繁体   中英

fixing apache v9.0 Server Tomcat v9.0 Server at localhost failed to start error

i have a simple java ee project which i need to send an email to a user, i found that it is possible with javamail and the gmail free smtp.

my mail sender implementation:

package duck.reg.pack;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@WebServlet("/sendmailtls")
public class SendMailTLS extends HttpServlet {
public static void main(){

final String username = "john.fob@gmail.com";
final String password = "Password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
});

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("joh.fob@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("jon.fob@yahoo.com"));
    message.setSubject("Testing Subject");
    message.setText("Dear Mail Crawler,"
        + "\n\n No spam to my email, please!");

    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}
}
}

now the problem is that when ever i compile my program i get apache error saying:

'Starting tomcat v9.0 Server at localhost' has encountered a problem. Server Tomcat v9.0 Server at localhost failed to start.

and i found that if i remove

Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password);
}
});

it compile just fine.

If there is a error in the project web.xml file (Not the server web.xml file) it prevents the starting the server starting.check and verify the web.xml and other xml files related to the project. For me when i fix the errors in web xml file server ran correctly.sometimes the server cache shows the previous errors in web pages which you are developing.beware of that also.

Apache server normally runs on port 8080 and this port might be busy so try to free that port . First find that which all process are running on port 8080 through cmd , command to find is

netstat -ano find "8080"

It will list the PID and using that PID kill that process through cmd, command to kill process is

taskkill /f /pid PIDno

In my case aa jar in .m2 folder was corrupted. I deleted it and did a maven install . It downloaded the jar again and this time my server was able to build

I had this error and i noticed that beneath the nav bar the button 'terminate' was coloured in red. I clicked it then the error was gone.

web.xml is the culprit here most of the time.

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Copy paste the content in your web.xml file

If you have defined " <?xml version="1.0" encoding="UTF-8"?> " by mistake remove it.

Remove the all @WebServlet annotations. Use web.xml file to handle the all mappings.

<servlet>
<servlet-name>xxx</servlet-name>
<servlet-class>xxx.xxx.xxx</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-pattern>/xxxxx</url-pattern>
</servlet-mapping> 

更改 tomcat 端口,转到 eclipse 上的服务器选项卡,右键单击->打开,然后更改 Tomcat 管理端口和 HTTP/1.1 端口,它们可能在其他地方使用。

kindly check all xml files again and cross check whether they are dependent on each other ,remove that all dependency or fix that dependency and after that follow the following steps 

1>right click project name
2>Run on server 
3>select installed tomcat version
4>then next 
5>remove all project from right side box 
6>keep only which u want to run
7>finish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM