Where do I put the JDBC driver? - java

I'm working on IntelliJ with Tomcat and postgresql but I have this problem when I run my application:
java.lang.ClassNotFoundException: org.postgres.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) and so on..
I have already put the JAR file in tomcat/lib but nothing changed.
Thanks for helping me.

You misspelled/shortened the qualified name of the PostgreSQL JDBC driver. The correct name is
org.postgresql.Driver

Make sure JDBC jar is added to the module dependencies: http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html.

Related

How to run oak-run console on postgres server

I am trying to connect to my DocumentNodeStore in a postgresql database.
Following the instruction from here https://jackrabbit.apache.org/oak/docs/features/oak-run-nodestore-connection-options.html
export CLASSPATH=postgresql-42.2.5.jar:oak-run-1.9.9.jar
java org.apache.jackrabbit.oak.run.Main console jdbc:postgresql://localhost/mydb --rdbjdbcuser myuser --rdbjdbcpasswd mypassword
I am getting the following error:
Apache Jackrabbit Oak 1.9.9
Exception in thread "main" org.apache.jackrabbit.oak.plugins.document.DocumentStoreException: trying to create datasource org.apache.tomcat.jdbc.pool.DataSource
at org.apache.jackrabbit.oak.plugins.document.rdb.RDBDataSourceFactory.forJdbcUrl(RDBDataSourceFactory.java:90)
at org.apache.jackrabbit.oak.plugins.document.rdb.RDBDataSourceFactory.forJdbcUrl(RDBDataSourceFactory.java:100)
at org.apache.jackrabbit.oak.run.cli.DocumentFixtureProvider.configureDocumentMk(DocumentFixtureProvider.java:117)
at org.apache.jackrabbit.oak.run.cli.NodeStoreFixtureProvider.create(NodeStoreFixtureProvider.java:70)
at org.apache.jackrabbit.oak.run.cli.NodeStoreFixtureProvider.create(NodeStoreFixtureProvider.java:49)
at org.apache.jackrabbit.oak.console.Console.main(Console.java:49)
at org.apache.jackrabbit.oak.run.ConsoleCommand.execute(ConsoleCommand.java:27)
at org.apache.jackrabbit.oak.run.Main.main(Main.java:49)
Caused by: java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSource
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.apache.jackrabbit.oak.plugins.document.rdb.RDBDataSourceFactory.forJdbcUrl(RDBDataSourceFactory.java:74)
... 7 more
Am I missing anything?
As mentioned in the link:
Note that the oak-run JAR file lacks several RDB specific JAR files that need to be added to the classpath:
1. tomcat-jdbc-8.5.*.jar (Apache Tomcat JDBC connection pool)
2. juli-6.0.*.jar (Apache Tomcat Logger)
3. Whatever JDBC driver is needed to connect to the database
I missed out the first 2 jars

Add jdbc postgre driver to jar

I'm building a java server program that connects to a psql database with JDBC drivers. I need to compile the program in a .jar file, but when I try to do so and run it, I get this exception. (the program has to run on a linux machine)
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Server.connectToDatabase(Server.java:53)
at Server.(Server.java:33)
at Main.main(Main.java:10)
Since the program works fine on IntelliJ editor, I assume that the problem is that there isn't the jdbc driver in the jar file. How can I add it?
You need add PostgreDriver to your classpath and use command like following.
$Java -classpath postgre-jar+additional-jars-if-any Your-Main-class

java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory

I am getting java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory when I start my web application.
As you can see in the image below, I have tomcat-juli.jar in my classpath for the Tomcat server configuration but it's still not working.
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
How can I solve this?
Put the tomcat-juli.jar in a path (the lib directory) that Tomcat use to load classes. If your Tomcat does not find it you can copy the jar in the lib directory of the JRE that you are using.
Okay, I had to add the tomcat-juli.jar to the classpath in the run configuration for the actual Java Application. All the answers I saw on the internet just pointed out that the Tomcat server needs this in his classpath (see my question).
Try export CLASSPATH=/usr/share/tomcat8/lib/tomcat-juli.jar.
You can find more information Here
I am seeing you are adding path as /usr/share/tomcat8/bin instead of /usr/share/tomcat8/lib/ Please check this also

MySql - Java - Class Not Found Exception

I am trying to use JDBC to access the MySql database on my computer. I get this error message
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at FunctionClass.special_function(FunctionClass.java:72)
at FunctionClass.<init>(FunctionClass.java:25)
at EvaluateFunctions.main(EvaluateFunctions.java:12)
I've seen on the other posts that this is due to the fact that the driver is not in the lib directory of the JDK. I tried that and it still doesn't work. I added the *.jar file to my "/usr/lib/default-java/lib/" folder on Xubuntu. I am not using any kind of servers. Here is my code that connects to the database:
Class.forName("com.mysql.jdbc.Driver");
Does anyone know what I am missing here?
Thanks for all the help in advance.
If you are using eclipse or some ide you can add the jar files to your build path.
It can be done by checking this link for eclipse. https://stackoverflow.com/a/27085441/4083590
If you are using command prompt you should add the jar files to your CLASSPATH. It can be done as shown below.
Linux:
CLASSPATH=$CLASSPATH;{Current working directory};{Direct Path to .jar file}
CLASSPATH=$CLASSPATH;/home/users/xyz/workspace/;/home/users/xyz/workspace/xyz.jar
Note: ./ can be used to signify current working directory
Windows:
set CLASSPATH=%CLASSPATH%:{Current working directory}:{Direct path to .jar file}
set CLASSPATH=%CLASSPATH%:C:\users\xyz\workspace:C:\users\xyz\workspace\xyz.jar
After doing this you would be able to compile your program.

Remote MYSQL on jasperreports error: com.mysql.jdbc.Driver not found

I am trying to use a remote MYSQL source for my jasperserver. The connection is good and does not throw an error. However, when I try to make a report in jasperstudio, I get the following error
Error log:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
net.sf.jasperreports.engine.JRRuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at net.sf.jasperreports.data.jdbc.JdbcDataAdapterService.getConnection(JdbcDataAdapterService.java:166)
at net.sf.jasperreports.data.jdbc.JdbcDataAdapterService.contributeParameters(JdbcDataAdapterService.java:126)
at com.jaspersoft.studio.data.jdbc.JDBCFieldsProvider.getFields(JDBCFieldsProvider.java:52)
at com.jaspersoft.studio.data.jdbc.JDBCDataAdapterDescriptor.getFields(JDBCDataAdapterDescriptor.java:83)
at com.jaspersoft.studio.data.ui.SimpleQueryWizardDataEditorComposite.readFields(SimpleQueryWizardDataEditorComposite.java:258)
at com.jaspersoft.studio.property.dataset.wizard.WizardDataSourcePage.run(WizardDataSourcePage.java:369)
at com.jaspersoft.studio.wizards.JSSWizardRunnablePage$1.run(JSSWizardRunnablePage.java:84)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at net.sf.jasperreports.engine.util.JRClassLoader.loadClassForRealName(JRClassLoader.java:175)
at net.sf.jasperreports.data.jdbc.JdbcDataAdapterService.getConnection(JdbcDataAdapterService.java:141)
... 7 more
I absolutely have the driver class mysql-connector-java-5.1.26-bin.jar in /opt/jasperreports-server-cp-5.5.0/lib too.
I also have the same connector.jar in the class path /opt/jasperreports-server-cp-5.5.0/webapps/jasperserver/WEB-INF/lib ... still nothing.
I'm not sure what the problem is...any help would be appreciated! Thanks!
SOLVED IT!!
I had to add the driver to the class path in jasper suite too!
sudo dnf install mysql-connector-java
updatedb
locate mysql-connector-java.jar
Then simply add the absolute path in your JasperSoft "Driver Classpath" tab.

Categories