473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

java.lang.NoClassDefFoundError catch-22

I have a test program that compiles but does not run.
Here is the code which I grabbed from from a web site:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.sql.*;
  6.  
  7. public class ColumnName{
  8.   public static void main(String[] args) {
  9.     System.out.println("Getting Column Names Example!");
  10.     Connection con = null;
  11.     String url = "jdbc:mysql://localhost:3306/";
  12.     String db = "jdbctutorial";
  13.     String driver = "com.mysql.jdbc.Driver";
  14.     String user = "root";
  15.     String pass = "root";
  16.     try{
  17.       Class.forName(driver);
  18.       con = DriverManager.getConnection(url+db, user, pass);
  19.       try{
  20.         Statement st = con.createStatement();
  21.         ResultSet rs = st.executeQuery("SELECT * FROM employee6");
  22.         ResultSetMetaData md = rs.getMetaData();
  23.         int col = md.getColumnCount();
  24.         System.out.println("Number of Column : "+ col);
  25.         System.out.println("Columns Name: ");
  26.         for (int i = 1; i <= col; i++){
  27.           String col_name = md.getColumnName(i);
  28.           System.out.println(col_name);
  29.         }
  30.       }
  31.       catch (SQLException s){
  32.         System.out.println("SQL statement is not executed!");
  33.       }
  34.     }
  35.     catch (Exception e){
  36.       e.printStackTrace();
  37.     }
  38.   }
  39. }
  40.  
Here are the runs:

1 WITH classpath NOT set

java ColumnName
Getting Column Names Example! <-output from line 9
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java: 217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at ColumnName.main(ColumnName.java:17)

RUN 2 with classpath set
java -classpath /opt/Java/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar ColumnName
Exception in thread "main" java.lang.NoClassDefFoundError: ColumnName
Caused by: java.lang.ClassNotFoundException: ColumnName
at java.net.URLClassLoader$1.run(URLClassLoader.java: 217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 21)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 66)
Could not find the main class: ColumnName. Program will exit.
Mar 8 '11 #1
1 3448
Hey,

Found the answer at another similar post here.
Or at least a work-around.

javac -classpath /opt/Java/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar ColumnName.java && java -classpath .:/opt/Java/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar ColumnName

Apparently, this is a classpath issue but I am happy with the work-around for now.
Mar 8 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Lem | last post by:
I get the error Exception in thread "main" java.lang.NoClassDefFoundError when I type java app2 in the command prompt. I've tried moving to the jre directory and typed java c:\app2\app2, but it...
1
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java...
0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
3
by: NathanK | last post by:
Hi Guys, im starting out in java and i'm getting the error: Exception in thread "main" java.lang.NoClassDefFoundError: blurf if i run "javac blurf.java", it compiles fine and without any...
1
by: greg.knaddison | last post by:
Hi, I'm trying to use the httpclient within Jython (see http://jakarta.apache.org/commons/httpclient/ for more information on the httpclient). My Jython version is: Jython 2.1 on...
3
by: Bernd Oninger | last post by:
When I run the successfully compiled java source shown at the end I got a runtime error: Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/Content Where is the missing class ?...
0
by: HiteshMehra | last post by:
Hi all, I am getting an error while trying to connect to the MQ. The error is java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at...
1
by: Nimesh dadhaniya | last post by:
Hi All, I am trying to learn EJB's on my own. I installed the WebLogic Server Trial version and then wrote the Home Interface, Component Interface and the Bean Class. Now I started the WebLogic...
4
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp...
1
by: rajujrk | last post by:
Hai All, I am Having a problem in the following... import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.io.FileUtils; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.