This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Exception in thread "main" java.lang.NoClassDefFoundError:" Watch "Exception in thread "main" java.lang.NoClassDefFoundError:" New topic
Author

Exception in thread "main" java.lang.NoClassDefFoundError:

Megha Singhal
Ranch Hand

Joined: Feb 28, 2012
Posts: 182

My code is following:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.sql.*;
import javax.naming.*;
import javax.naming.directory.*;
import oracle.jdbc.pool.OracleDataSource;

public class Connectivity {

public static void main(String args[]) {
Connection con = null;


try {

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/racltc");
if (envContext == null)throw new Exception("Error: No Context");
if (ds == null) throw new Exception("Error: No DataSource");
if (ds != null)
con = ds.getConnection();
if(!con.isClosed())
System.out.println("Successfully connected to " +
"JNDI server using TCP/IP...");

} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}

and i am getting below error, i don't know why. although i set all the paths and classpath
Exception in thread "main" java.lang.NoClassDefFoundError: Connectivity
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1400
    
    6

Megha,

Could you please paste your code inside code tags. As far as the error is concerned it looks like a classpath problem.


Swastik
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Yes, the issue is with how you're running this code.

import oracle.jdbc.pool.OracleDataSource;

You should generally not import the DB-specific classes of a JDBC driver. What do you hope to achieve by that?

} catch(SQLException e) {}

And you should never ignore I/O or DB exceptions. At the very least, log the exception to where you will see it. In production code you should also handle any such exception.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

The JVM cannot find your Connectivity.class file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Megha Singhal wrote:and i am getting below error, i don't know why. although i set all the paths and classpath
Exception in thread "main" java.lang.NoClassDefFoundError: Connectivity

Is the current folder (.) still a part of your class path? It is by default, but as soon as you manually specify a class path you must include it manually.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mallik kannan
Greenhorn

Joined: Sep 07, 2009
Posts: 4
In one of the reply for this issue, they replied as below:

"Is the current folder (.) still a part of your class path? It is by default, but as soon as you manually specify a class path you must include it manually."

Que: My classpath : "C:\Program Files\Java\jdk1.6.0_31\bin"

Could you please elaborate, i couldnt understand this...

Steps i tried in my pc:

I installed Java in C drive, and i'm storing all .java files and .class files in C drive. And while Compiling using Command Prompt with 'javac' command as "javac <filename.java>", it compiles without showing any exception, but when i run the .class file,using command "java <filename>", it shows below exception as;

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
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)
Could not find the main class: HelloWorld. Program will exit.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

You don't need to set your class path at all unless you need some libraries to be part of it. The bin folder of your JRE or JDK should never be a part of it; the JDK's bin folder should be part of the PATH variable, not CLASSPATH.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NoClassDefFoundError:
 
Similar Threads
JNDI Lookup help in websphere 4.0
db access through applet
Statement Closed Error - please help
mysql connectivity with jboss
adding a Datasource to tomcat jndi