当前位置: 技术问答>java相关
java可以编译程序运行不了main方法。怎么回事啊?
来源: 互联网 发布时间:2015-08-10
本文导语: import java.sql.*; import java.util.*; public class test1 { public Connection con = null; public Statement smt = null; public ResultSet sqlRst; public test1() { try { Class.forName("com.microsoft...
import java.sql.*;
import java.util.*;
public class test1
{
public Connection con = null;
public Statement smt = null;
public ResultSet sqlRst;
public test1()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
System.out.println("connect to the database");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://sancy:1433;DatabaseName=bbs", "sa", "8888");
smt=con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
System.out.println("connect to the database success");
}
catch (Exception e)
{
System.out.println("connect to the database error");
//System.out.println(e.toString());
}
}
public ResultSet getRs(String questr)
{
try
{
System.out.println("executeQuery");
sqlRst = smt.executeQuery(questr);
System.out.println("after smt.executeQuery");
System.out.println("no error");
}
catch(Exception e)
{
System.out.println("error");
return null;
}
return sqlRst;
}
public static void main(String args[])
{
test1 t1=new test1();
ResultSet rs=t1.getRs("select top 10 * from company where len(cname)>10");
if (rs!=null)
{
try
{
while (rs.next())
{
System.out.println(rs.getString("cname"));
}
}
catch(Exception e)
{
System.out.println("error");
}
}
}
}
以上这段代码编译通过。而且class文件在JSP页面通过usebean引用也使用正常。(tomcat环境)
但是单独运行命令java test1却出错:
Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/jdbc/base/BaseDriver
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at test1.(test1.java:12)
at test1.main(test1.java:44)
就是说他在运行测试main()的时候找不到sql驱动,但在tomcat下却很正常(能返回记录集)这是怎么回事啊?该怎么设置?在线等待......
import java.util.*;
public class test1
{
public Connection con = null;
public Statement smt = null;
public ResultSet sqlRst;
public test1()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
System.out.println("connect to the database");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://sancy:1433;DatabaseName=bbs", "sa", "8888");
smt=con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
System.out.println("connect to the database success");
}
catch (Exception e)
{
System.out.println("connect to the database error");
//System.out.println(e.toString());
}
}
public ResultSet getRs(String questr)
{
try
{
System.out.println("executeQuery");
sqlRst = smt.executeQuery(questr);
System.out.println("after smt.executeQuery");
System.out.println("no error");
}
catch(Exception e)
{
System.out.println("error");
return null;
}
return sqlRst;
}
public static void main(String args[])
{
test1 t1=new test1();
ResultSet rs=t1.getRs("select top 10 * from company where len(cname)>10");
if (rs!=null)
{
try
{
while (rs.next())
{
System.out.println(rs.getString("cname"));
}
}
catch(Exception e)
{
System.out.println("error");
}
}
}
}
以上这段代码编译通过。而且class文件在JSP页面通过usebean引用也使用正常。(tomcat环境)
但是单独运行命令java test1却出错:
Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/jdbc/base/BaseDriver
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(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.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at test1.(test1.java:12)
at test1.main(test1.java:44)
就是说他在运行测试main()的时候找不到sql驱动,但在tomcat下却很正常(能返回记录集)这是怎么回事啊?该怎么设置?在线等待......
|
.NoClassDefFoundError!!!
应该是缺少了某些包了
我这里的j2sdk的编译环境和运行环境在两个不同的目录下,所以我加入新的包的时候,如果没有两个一起加的话,就会出现能编译,但是运行是出现NoClassDefFoundError!!!错误
应该是缺少了某些包了
我这里的j2sdk的编译环境和运行环境在两个不同的目录下,所以我加入新的包的时候,如果没有两个一起加的话,就会出现能编译,但是运行是出现NoClassDefFoundError!!!错误