Archive for the ‘Java’ Category

java.lang.NoClassDefFoundError when loading a class located in another project at Eclipse

It is useful to use classes that resides in another project in the Eclipse Workspace. In order to avoid java.lang.NoClassDefFoundError error, perform the following steps: Add the project that has the wanted classes to the Java Build Path Properties-> java build path -> projects ->add your project that contain wanted classes Check the added project […]

How to Build a RESTful Web Service with JAX-WS and Jersey on Eclipse

In order to build a RESTful API with Jersey on Eclipse, please use the following steps: Create new Eclipse Dynamic Web Project Open Eclipse, then File -> New -> Dynamic Web Project. Name it “TestProject”. Generating web.xml file Generate a web.xml file IN WebContent/WEB_INF by: Right click on the created project -> Java EE Tools […]

Hibernate: How to insert OneToMany children by cascade

I needed a way to insert an entity with a new joined (children) entity together. One way to do it is by separate the actions (e.g. to save the joined entity first and then to save the main entity). However, Hibernate support inserting a new entity with new joined entity. I would like to present […]

Connection Pool: MySQL Communications link failure

The Problem And The Solution While using a MySQL connection pool in Java, I received a MySQL Communications link failure Exception (see below). In order to solve communication link failure exception: I have removed JDBC property autoReconnect=true and put only the JDBC property autoReconnectForPools=true I have added the connection properties: testOnBorrow testWhileIdle timeBetweenEvictionRunsMillis minEvictableIdleTimeMillis See […]

Connection Pool: MySQL connection closed exception

The Problem While using a MySQL connection pool in Java, I received a MySQL Connection Closed Exception (see below). The problem was that the JDBC driver was not compatible to the MySQL server version. I have used version 5.05a for both JDBC and MySQL and it solved the problem The problem is that the connection […]

How To Switch Java in Centos

* If you need to install a new version of java, download the version from java.sun * run /usr/sbin/alternatives to change the default java. If you will run the next command you will see which java versions are available: # /usr/sbin/alternatives –config java There is 1 programs which provide ‘java’. Selection Command ———————————————– *+ 1 […]

Adding DbUnit to your project

About DbUnit DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to […]

How to set up a MySQL connection pool in Java

MySQL connection pool A MySQL connection pool is a pool of connections to MySQL database. Opening and maintaining a database connection for each process (or thread), is time costly (connection creation time) and wastes resources (connections). Connection pool increase the performance of (Java) applications that needs to connect to the database by reusing the connections. […]

How to install ANT on LINUX

Apache Ant is a Java library and command-line tool that help building software. How to install: Create downloads directory if you dont have one Download tar version from Extract Rename the new directory as ant Insert the path into ANT_HOME Update the global PATH variable to include ANT_HOME Run the Ant script fetch.xml to […]

How to dynamically create an object in Java from a class name given as string format

To create an object dynamically from the class name, you need to use a reflection. If the fully-qualified name of a class is available, it is possible to get the corresponding Class using the static method Class.forName(). However, This cannot be used for primitive types. Having the Class type, you need to use the newInstance() […]