0

I have developed an application which is used to log record into Database with log4j2.xml using JDBCAppender. I have followed the following instruction here :- http://self-learning-java-tutorial.blogspot.in/2015/10/log4j2-jdbcappender-write-log-messages.html
My DB ConnectionFactory class is as follows:-

import java.sql.Connection;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;
    public class ConnectionFactory {
        private static interface Singleton {
            final ConnectionFactory INSTANCE = new ConnectionFactory();
        }

        private final DataSource dataSource;

        private ConnectionFactory() {
            Properties properties = new Properties();
            properties.setProperty("user", "sa");
            properties.setProperty("password", "Login@123"); // or get properties from some configuration file

            GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
            DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
                    "jdbc:sqlserver://SERV1-PC\\SQLEXPRESS:1433;databaseName=Anirban;", properties
            );
            new PoolableConnectionFactory(
                    connectionFactory, pool, null, "SELECT 1", 3, false, false, Connection.TRANSACTION_READ_COMMITTED
            );

            this.dataSource = new PoolingDataSource(pool);
        }
       public static Connection getDatabaseConnection() throws SQLException {
            return Singleton.INSTANCE.dataSource.getConnection();
        }
    } 

and my log4j2.xml snippet is as follows:-

<Appenders>
 <JDBC name="databaseAppender" tableName="LogTable">
   <ConnectionFactory class="ConnectionFactory" method="getDatabaseConnection" />
   <Column name="EVENT_DATE" isEventTimestamp="true" />
   <Column name="LEVEL" pattern="%level" />
   <Column name="LOGGER" pattern="%logger" />
   <Column name="MESSAGE" pattern="%message" />
   <Column name="THROWABLE" pattern="%ex{full}" /> 
  </JDBC>

</Appenders>

<AsyncRoot level="INFO" > 
<AppenderRef ref="databaseAppender" /> 
</AsyncRoot>

Now everything works fine and I can see the log in DB.

But when I export this project as a jar file in order to make it a common shared library jar and use it in other applications as dependency, I am encountered with ClassNotFoundException.

So, whenever I start the application that has this shared jar file as dependency, I get the following :-

ERROR java.lang.ClassNotFoundException: ConnectionFactory java.lang.ClassNotFoundException: ConnectionFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method) 

I a not sure why this ConnectionFactory java class is not getting into the classpath in the shared jar, but works fine when this is run as an application mentioned above.

Please suggest, how to include the ConnectionFactory java class in classpath in the jar? So do I need to do an modification in my log4j2.xml?

Sri Arun
  • 133
  • 11
  • As an aside, using the default package is a bad practice. However, you have not shown us the entire ConnectionFactory.java file, so it is possible that you _have_ declared a package, which would be the cause of your problems. – rmlan Nov 29 '16 at 18:30
  • I agree default package is a bad practice. I am doing a POC and I actually shifted the java class from a package to default. anyways this is the entire JAVA code pasted – Sri Arun Nov 29 '16 at 18:35
  • edited and pasted the code – Sri Arun Nov 29 '16 at 18:37
  • What does "export this project as a jar file" and "make it a common shared library jar" mean? You haven't given us enough information to give you a good answer. – rmlan Nov 29 '16 at 18:39
  • I need to make it as a JAR file, that I can use as a common jar and put into any other application as dependency. So, when an application which has used this jar as a dependency get deployed under the server, the log will be inserted into DB – Sri Arun Nov 29 '16 at 18:44
  • Right, but those are vague concepts. How you employ them is very important to your problem. More details are necessary. First, how are you actually creating the jar? Second, where are you putting this jar file that makes you think it is available to the application you have developed? – rmlan Nov 29 '16 at 18:45
  • right now I am not using maven .. I plan to use it but for now I am exporting as a jar application directly from my eclipse – Sri Arun Nov 29 '16 at 18:51
  • Please update the code, the log4j2.xml and the exception trace to include the package. Also better show the full Log4j2 configuration rather than a snippet. – Remko Popma Nov 29 '16 at 22:35
  • Finally, add a section showing how you are using the exported jar: full classpath, what is the code of the application using the jar, and configuration (same Log4j2 configuration?) etc. – Remko Popma Nov 29 '16 at 22:37

3 Answers3

0

Have you selected all the resources while exporting jar in eclipse? Also, check out if it helps : Java: export to an .jar file in eclipse

Community
  • 1
  • 1
dhamu
  • 605
  • 1
  • 7
  • 17
  • Done the same... but no luck :( – Sri Arun Nov 29 '16 at 20:14
  • How are you adding this jar as a dependency in another application? If another application which has this dependency is a jar file, try adding dependency with -classpath: – dhamu Nov 30 '16 at 15:52
  • (Continuing previous comment) : java -classpath dependency1.jar;subfolder/dependency2.jar;myapp.jar package.of.your.main.Class or if its a webapp, try adding jar to tomcat_dir/WEB-INF/lib – dhamu Nov 30 '16 at 15:53
0

Can you try using the Fully Qualified class name ? Also, check that the class is accessible from the xml path.

Vijay
  • 384
  • 4
  • 16
  • Yes... it's actually working when it is run as an application. but when used as a jar on other application, it doesn't work :( – Sri Arun Nov 29 '16 at 20:17
0

You need to make a executable jar file. Follow the link : http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-37.htm

arjun99
  • 358
  • 1
  • 8