【问题标题】:FileNotFoundException in Eclipse after Java updatedJava 更新后 Eclipse 中的 FileNotFoundException
【发布时间】:2013-03-17 18:26:46
【问题描述】:

我有一个程序,其中客户端上传文件并将文件存储在数据库中。在我的计算机上更新 Java 之前,我的项目运行良好。现在我收到了FileNotFoundException。 Eclipse 告诉我:

JAR 文件 C:\Program Files\Java\jre7\lib\rt.jar 没有源 附件。

有人遇到过这个问题吗?

我上传文件的servlet代码是标准的:

if (!ServletFileUpload.isMultipartContent(request))
    {return;}

try 
{ 
  List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
  courseID = items.get(0).getString();
  homeworkID = items.get(1).getString();
  File storeFile = new File(items.get(2).getName());
  String fileName = storeFile.getName();
  String mimeType = new MimetypesFileTypeMap().getContentType(storeFile); //for putting file into database
  putFileIntoDB(uName, courseID, homeworkID, fileName, mimeType, storeFile);
}//try
 catch (Exception ex)
 {
   request.setAttribute("msg", "There was an error: " + ex.getMessage());
   request.getRequestDispatcher("/upload.jsp").forward(request, response);
  }

在fis=new FileInputStream(f)这行创建FileInputStream时putFileIntoDB方法有错误:

void putFileIntoDB(String username, String courseID, String homeworkID, String fileName, String mime, File f) throws SQLException, IOException
{   
    FileInputStream fis = null;
    java.sql.PreparedStatement pstmt = null;
    java.sql.Connection conn = null;
    try {
            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);

            fis = new FileInputStream(f);

            pstmt = conn.prepareStatement("insert into files(username, courseID, assignmentID, fileName, mimeType, contents) values (?, ?, ?, ?, ?, ?)");
            pstmt.setString(1, username);
            pstmt.setString(2, courseID);
            pstmt.setString(3, homeworkID);
            pstmt.setString(4, fileName);
            pstmt.setString(5, mime);
            pstmt.setAsciiStream(6, fis, (int) f.length());

            pstmt.executeUpdate();
            conn.commit();
        }
    catch (Exception e)
    {
      System.err.println("Error: " + e.getMessage());
      e.printStackTrace();
    } 
    finally
    {
      pstmt.close();
      fis.close();
      conn.close();
    }
}

还有其他人遇到过这个问题吗?

【问题讨论】:

  • 您是否检查过该 jar 在该位置是否可用?
  • 是的,当我导航到该位置时,rt.jar 文件就在那里。这就是为什么我不明白为什么它告诉我它没有源附件。

标签: java servlets file-upload


【解决方案1】:

尝试以下步骤;

第 1 步 - 将 Ecilpse 配置为使用 JDK 而不是 JRE。

Window->Preferences->Java->Installed JREs
Click "Add", "Standard VM", "Next"
For "JRE Home" click "Directory"
Locate your JDK directory...
Ex: C:\java\jdk1.6.0_24
Click "Finish"
Next check the JDK vs the JRE that is listed.
Click "Ok"

第 2 步 - 将您的项目配置为使用 JDK,因为它是使用 JRE 作为“已安装的 JRE”创建的。新项目应该没问题。

Right click the project's name.
Choose "Properties", "Java Build Path"
Click the tab "Libraries".
Click "Add Libraries", select "JRE System Library", Next
Select the bottom item: Workspace default JRE (jdk1.x.x. etc....)
(Notice that it is now a JDK and not the JRE!)
Click "Finish"
Now remove the JRE libarary so you only have the JDK library.
ex: JRE System Library [JavaSE-1.6]
Click "OK"

【讨论】:

  • 好吧,我试过了,不幸的是我仍然遇到同样的问题。我不知道我的 Eclipse 设置发生了什么。我正在考虑卸载它并重新安装它。你知道是否有办法恢复到以前的工作时间?
  • 不,您无法还原。在此处查看类似问题:stackoverflow.com/questions/10367286/…
猜你喜欢
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 2011-01-08
  • 2015-02-06
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
相关资源
最近更新 更多