File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Megha Singhal
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Megha,

Could you please paste your code inside code tags. As far as the error is concerned it looks like a classpath problem.
 
Tim Moores
Bartender
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM cannot find your Connectivity.class file.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mallik kannan
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic