如何修复此“包不存在”错误?

时间:2010-10-20 01:28:02

标签: java

在OS X上。

$ java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02-279-10M3065)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01-279, mixed mode)

这是我的整个项目:

$ ll
total 1072
-rw-r--r--  1 ethan  ethan    1316 Oct 19 17:17 BlackberryDataExtractor.class
-rw-r--r--  1 ethan  ethan    1439 Oct 19 18:05 BlackberryDataExtractor.java
-rw-r--r--@ 1 ethan  ethan  537303 Apr 19  2010 sqljdbc4.jar
                                                    <-- MS SQL Server JDBC driver

这是BlackberryDataExtractor.java。它还没有真正做任何事情。我只是想让基本的东西像数据库连接一样工作:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.microsoft.sqlserver.jdbc;

class BlackberryDataExtractor {

  public static void main(String[] args) {
    Connection con = BlackberryDataExtractor.getConnection();
  }

  public static final Connection getConnection()
  {
    Connection con = null;
    try
    {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = DriverManager.getConnection("jdbc:sqlserver://[ etc... ]");
      String SQL = "SELECT uc.MailboxSMTPAddr AS email, sd.PhoneNumber AS phone_number"
        + "FROM dbo.UserConfig uc, dbo.SyncDeviceMgmtSummary sd"
        + "WHERE sd.UserConfigId = uc.Id";
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(SQL);

      while (rs.next()) {
        System.out.println(rs.getString("email") + ", " + rs.getString("phone_number"));
      }
      rs.close();
      stmt.close();

    }
    catch(SQLException e)
    {
      e.printStackTrace();
    }
    catch(ClassNotFoundException e)
    {
      e.printStackTrace();
    }
    return con;
  }
}

它不会编译。这是错误:

$ javac -cp "$CLASSPATH;*.jar" BlackberryDataExtractor.java
BlackberryDataExtractor.java:4: package com.microsoft.sqlserver does not exist
import com.microsoft.sqlserver.jdbc;
                              ^
BlackberryDataExtractor.java:23: cannot find symbol
symbol  : class Statement
location: class BlackberryDataExtractor
      Statement stmt = con.createStatement();
      ^
BlackberryDataExtractor.java:24: cannot find symbol
symbol  : class ResultSet
location: class BlackberryDataExtractor
      ResultSet rs = stmt.executeQuery(SQL);
      ^
3 errors

3 个答案:

答案 0 :(得分:2)

javac不了解*.jar之类的通配符。你必须明确。

尝试javac -cp "$CLASSPATH;sqljdbc4.jar" ...

答案 1 :(得分:1)

是否直接指定jar文件而不是使用glob“* .jar”工作?

javac -cp "$CLASSPATH;sqljdbc4.jar" BlackberryDataExtractor.java

看起来你还缺少一些java.sql。进口。 (ResultSet和Statement)

答案 2 :(得分:0)

我有同样的问题。当我在Eclipse中编写和执行程序时,一切正常,但是当我在终端(Ubuntu)中执行程序时,出现错误“包com.microsoft.sqlserver不存在”。我不确定这是否是一个好的解决方案,但我将项目导出到jar文件,然后运行java -jar "filename".jar.,然后一切正常。