1

Trying to create a basic login page in Java using IntelliJ. I've added the JAR to the CLASSPATH using these instructions: Adding Jar files to IntellijIdea classpath. The error I'm getting is below.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at main.Validate.checkUser(Validate.java:17)
at main.Login.doPost(Login.java:23)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1441)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
20-Mar-2017 16:14:19.453 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory D:\Users\MYNAME\apache-tomcat-8.5.12\webapps\manager
20-Mar-2017 16:14:19.523 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\Users\MYNAME\apache-tomcat-8.5.12\webapps\manager has finished in 70 ms

The class that connects to the DB is called Validate;

package main;

import java.sql.*;

public class Validate
{
    public static boolean checkUser(String username,String password)
    {

        boolean st =false;
        try{

            //loading drivers for mysql
            Class.forName("com.mysql.jdbc.Driver");

            //creating connection with the database
            Connection con=DriverManager.getConnection
                    ("jdbc:mysql:/ /localhost:3306/wpdcourseworkdb","root","");
            PreparedStatement ps =con.prepareStatement
                    ("select * from users where username=? and password=?");
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs =ps.executeQuery();
            st = rs.next();

        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return st;
    }
}
Community
  • 1
  • 1
CDickson
  • 195
  • 2
  • 5
  • 15
  • Besides adding a new project library, did you assign the library to a module as well? Right-click on library -> _Add to Modules..._ . – Luciano van der Veekens Mar 21 '17 at 12:58
  • Do note that by adding a library to IntelliJ's classpath, the library is not available when running the program outside of the IDE, e.g. in a different JVM. – Luciano van der Veekens Mar 21 '17 at 13:03
  • @luc14n0 - I have assigned the Library to the module as a dependancy. http://imgur.com/a/R072v This is what my Libraries and Modules look like currently. The 2 problems are the Libraries being missing from the artifact. – CDickson Mar 21 '17 at 13:48

1 Answers1

0

Adding libraries to IntelliJ's classpath doesn't add them to the build artifact, e.g. the .jar file. If you're using Maven, you should add the dependency to the POM file and use the maven-assembly-plugin to create a fat archive containing all needed dependencies:

https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

EDIT: after a discussion in the chat it turned out the user was trying to deploy a simple .jar on tomcat whereas he thought he was deploying a web archive. The thin .jar didn't contain the required dependencies and tomcat also didn't supply them as part of its core.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • I am using maven, and I have the dependency for the MySQL connector in my pom.xml. I tried using the maven-assembly-plugin, following the instructions in the link you gave, but it doesn't seem to have worked. – CDickson Mar 21 '17 at 16:45
  • @CDickson can you attach the POM file in the question? – Luciano van der Veekens Mar 21 '17 at 18:17
  • @CDickson your configuration looks okay. Are you using the correct archive? The maven-assembly-plugin creates an additional archive ending in _jar-with-dependencies.jar_ which is the one you want to use. – Luciano van der Veekens Mar 22 '17 at 09:01
  • I was not! Found it in my target folder however, so now I know if it can I check where its meant to go? Into libraries? – CDickson Mar 22 '17 at 09:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138721/discussion-between-luc14n0-and-cdickson). – Luciano van der Veekens Mar 22 '17 at 10:21